saving text values of dynamically created textboxes

Y

YunusEmre

I have many textboxes that are created dynamically as child controls of my
custom control.

I know that I must recreate them after each postback and also know that I
can use the ViewState property to save the state of my controls.

What I dont know is when to save the values into viewstate. I want to save
the text in the textboxes. Since it is the user that enters the text into
those, I need some event like OnJustBeforePostBack to save the latest values
in the textboxes.

Any help will be appreciated.
Thanks in advance...

Emre
 
R

R. Thomas, aka Xtreme.Net

Whenever a event takes place, i.e when user presses the submit button, you
should store the values of your textboxes to the viewstate. I think that
should be safe enough...
Hth...
R. Thomas
 
Y

YunusEmre

I guess that will not be enough because the OnClick event handler of the
button is executed AFTER the postback so my dynamic control is already gone
and recreated and so its value is already wiped out.

Emre
 
Y

YunusEmre

In my situation there is no DB. I store the info that is used to create the
TextBox also in the ViewState.
After reading the article I tried something. I put the following code...

protected override void OnLoad(EventArgs e)

{

base.OnLoad (e);

TrialCtrl= new TextBox();

this.Controls.Add( TryCtrl );

TrialCtrl.ID = "trialctrl";

}



....into my class as an OnLoad event handler and it worked and also didn't
work. What worked is that the value that the user enters into the TextBox
survives the first PostBack, but not the others. After the second and
following postbacks the text is lost again. ... makes no sense ?!?? Any idea
why this happens ?
 
Y

YunusEmre

the article talk about creating the control in the OnLoad event handler. And
since the OnLoad event handler runs after the LoadViewState stage, I can
have my data in the viewstate that will be used to create the controls.
If I can get the code below run as it should run, I guess creating the
controls in the OnLoad event handler will solve my problem...

YunusEmre
 
R

R. Thomas, aka Xtreme.Net

So you managed to solve it?
P.S : If this post was helpful, pls click 'Yes' on top of this post. Thanks
R. Thomas
 
Y

YunusEmre

I am afraid not, as I said earlier, the value in the textbox(TrialCtrl)
survives the first PostBack but not the second one or the ones after that...
After the second postback the text in the textbox is gone and any other
thing you write there after that stage doesn't persist through postbacks any
more.
 
R

R. Thomas, aka Xtreme.Net

I made a simple aspx page an put this in my onload
TrialCtrl= new TextBox();
form.Controls.Add( TrialCtrl );
TrialCtrl.ID = "trialctrl";
with public TextBox TrialCtrl; declared.
It works good for me... the values stays there after every postback...
I think they way you are overloading and inheritting has some problems..
You were saying you are overloading right?
Lets look into that.. cause the above code i showed u works perfectly fine...
Hth...
R. Thomas
 
Y

YunusEmre

you are right. It works well on a clean page. Something I do is probaby
making it fail. I will try to figure out what it is and get back to you.
Thanks...
Emre
 
Y

YunusEmre

after some painful hours before the screen I realized that it is not the
postback that makes it fail but it is the DataBind(). The postbacks which
call the DataBind method eventually make my TrialCtrl fail to persist its
text through postbacks.
I have some buttons on my custom control (which is a grid by the way), some
of them are used for paging and sorting stuff. And naturally the event
handlers of these buttons call the DataBind() sometime after doing that
"paging and sorting stuff". When DataBind() is called, it somehow causes the
text in the textbox to be deleted.
 
R

R. Thomas, aka Xtreme.Net

So, is everything ok now?
R. Thomas

YunusEmre said:
after some painful hours before the screen I realized that it is not the
postback that makes it fail but it is the DataBind(). The postbacks which
call the DataBind method eventually make my TrialCtrl fail to persist its
text through postbacks.
I have some buttons on my custom control (which is a grid by the way), some
of them are used for paging and sorting stuff. And naturally the event
handlers of these buttons call the DataBind() sometime after doing that
"paging and sorting stuff". When DataBind() is called, it somehow causes the
text in the textbox to be deleted.
 
R

R. Thomas, aka Xtreme.Net

There's gotta be something that you are doin wrong in ur code...
Maybe you'd like to post some code.??
R. Thomas
 
R

R. Thomas, aka Xtreme.Net

There's gotta be something that you are doin wrong in ur code...
Maybe you'd like to post some code.??
R. Thomas
 
Y

YunusEmre

I finally figured it out. The call to Databind clears the Controls
collection. I overrided DataBind method and after calling the base class's
DataBind I called my function which adds the controls to the collection
again.

Thus, after DataBind clears the controls collection I add put them there
again. This solved my problem.

Thank you very much for your attention. I really appreciate it.
Thanks a lot.
Emre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top