saving text values of dynamically created textboxes

Discussion in 'ASP .Net Building Controls' started by YunusEmre, Dec 28, 2004.

  1. YunusEmre

    YunusEmre Guest

    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
     
    YunusEmre, Dec 28, 2004
    #1
    1. Advertisements

  2. 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
     
    R. Thomas, aka Xtreme.Net, Dec 28, 2004
    #2
    1. Advertisements

  3. YunusEmre

    YunusEmre Guest

    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
     
    YunusEmre, Dec 28, 2004
    #3
  4. R. Thomas, aka Xtreme.Net, Dec 29, 2004
    #4
  5. YunusEmre

    YunusEmre Guest

    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 ?
     
    YunusEmre, Dec 30, 2004
    #5
  6. YunusEmre

    YunusEmre Guest

    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
     
    YunusEmre, Dec 30, 2004
    #6
  7. So you managed to solve it?
    P.S : If this post was helpful, pls click 'Yes' on top of this post. Thanks
    R. Thomas
     
    R. Thomas, aka Xtreme.Net, Dec 30, 2004
    #7
  8. YunusEmre

    YunusEmre Guest

    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.
     
    YunusEmre, Dec 30, 2004
    #8
  9. 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
     
    R. Thomas, aka Xtreme.Net, Dec 30, 2004
    #9
  10. YunusEmre

    YunusEmre Guest

    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
     
    YunusEmre, Dec 30, 2004
    #10
  11. YunusEmre

    YunusEmre Guest

    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.
     
    YunusEmre, Dec 31, 2004
    #11
  12. So, is everything ok now?
    R. Thomas

     
    R. Thomas, aka Xtreme.Net, Dec 31, 2004
    #12
  13. YunusEmre

    YunusEmre Guest

    No, the call to DataBind still does harm and I don't know why it does that.
     
    YunusEmre, Dec 31, 2004
    #13
  14. There's gotta be something that you are doin wrong in ur code...
    Maybe you'd like to post some code.??
    R. Thomas
     
    R. Thomas, aka Xtreme.Net, Jan 3, 2005
    #14
  15. There's gotta be something that you are doin wrong in ur code...
    Maybe you'd like to post some code.??
    R. Thomas
     
    R. Thomas, aka Xtreme.Net, Jan 3, 2005
    #15
  16. YunusEmre

    YunusEmre Guest

    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
     
    YunusEmre, Jan 3, 2005
    #16
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.