Accessing Textbox Text After Postback in Dynamic Control

Discussion in 'ASP .Net Building Controls' started by Elroyskimms, Feb 20, 2006.

  1. Elroyskimms

    Elroyskimms Guest

    I have an aspx page, which loads a custom usercontrol (UCtrlA). UCtrlA
    has a drop down list, and depending on what is selected in UCtrlA's
    drop down list ("Create New" or "Edit Existing"), another usercontrol
    is added (dynamically) to UCtrlA through LoadControl (UCtrlNew or
    UCtrlEdit).

    UCtrlNew has some textboxes and a submit button. When the submit button
    is clicked, the contents of the textboxes are combined and saved in a
    text file on the server.

    UCtrlEdit has the same basic controls as UCtrlNew (textboxes and an
    update button) and is supposed to be used to read the existing text
    file (created by UCtrlNew) and re-populate the textboxes with their
    original values. The update button should combine the textbox values
    and overwrite the original text file on the server, thus updating it.
    When UCtrlEdit is loaded (OnLoad), the values are read properly from
    the text file and are loaded into their appropriate textboxes. However,
    when I click the update button the contents of the textboxes are
    returned to their original values (as read from the textfile). For
    example, textfile reads "Original text" and when UCtrlEdit loads, the
    textbox shows "Original text". I change the value to be "Modified text"
    and click submit. When the postback is complete, the textbox shows
    "Original text".

    If this were a web page, the solution is to only read the textfile when
    the page is not being loaded via postback. However, this is a dynamic
    control so the control is first loaded on a postback and every
    interaction with the control will be done through a postback. Somehow,
    I need to access the modified contents of the textbox. I've read in
    various places that I should populate the textbox in the OnInit
    procedure but that has not resolved the problem (perhaps I am doing it
    wrong?). When the Update button is clicked, are the contents of the
    modifed textboxes stored in ViewState or does ViewState only contain
    the original values read from the textfile? If the modified text is in
    ViewState, how do I search ViewState for the contents of a specific
    textbox? If ViewState is not the answer, how do I access the modified
    version of a textbox after postback in a dynamic control?
     
    Elroyskimms, Feb 20, 2006
    #1
    1. Advertising

  2. Elroyskimms

    Elroyskimms Guest

    I found a workaround. By adding a public boolean variable to my
    usercontrol (UCtrlEdit), I can check the value of that variable to
    determine if I should read the text file or not. The variable is set to
    true when the control is loaded from the drop down list. Otherwise, the
    variable is not set to true when the control is loaded on the postback.
    This "solves" my problem but I don't think this is the best way to
    accomplish this. What is the "proper" solution to my problem?
     
    Elroyskimms, Feb 20, 2006
    #2
    1. Advertising

  3. Elroyskimms

    Elroyskimms Guest

    OK, so I lied about the workaround. This boolean switch works to
    prevent the contents of the text file from being read but now the
    textbox is blank after clicking the Update button, which tells me the
    viewstate of the textbox is not being preserved on postback. Before you
    ask, viewstate is enabled for the textbox and all pages. How can I
    preserve the viewstate of this usercontrol? Any help would be
    apprecatied. Thanks!
     
    Elroyskimms, Feb 20, 2006
    #3
  4. Elroy... I found this in google and used it with success

    http://groups.google.com/group/micr...t&q=isfirstload&rnum=1&hl=en#3fddbe93a2d28066

    now...are you loading these controls in a placeholder/panel? you may
    consider using this placeholder that keeps track of its own viewstate.

    http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

    hth

    Jose

    "Elroyskimms" <> wrote in message
    news:...
    > OK, so I lied about the workaround. This boolean switch works to
    > prevent the contents of the text file from being read but now the
    > textbox is blank after clicking the Update button, which tells me the
    > viewstate of the textbox is not being preserved on postback. Before you
    > ask, viewstate is enabled for the textbox and all pages. How can I
    > preserve the viewstate of this usercontrol? Any help would be
    > apprecatied. Thanks!
    >
     
    Jose Rodriguez, Feb 22, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Jones
    Replies:
    4
    Views:
    1,345
    Jeremy Jones
    Dec 14, 2006
  2. Replies:
    7
    Views:
    1,256
  3. zhixuen
    Replies:
    0
    Views:
    1,236
    zhixuen
    Feb 10, 2009
  4. News User

    textbox text missing after detailsview postback?

    News User, Jul 31, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    452
    Eliyahu Goldin
    Jul 31, 2006
  5. randman
    Replies:
    3
    Views:
    487
Loading...

Share This Page