Accessing Textbox Text After Postback in Dynamic Control

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Accessing Textbox Text After Postback in Dynamic Control

    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 Guest

  2. Similar Questions and Discussions

    1. Dynamic control state changes lost on Postback
      I have been developing a set of custom controls that include several dynamically generated controls inside them (for example, 'SupportTextBox'...
    2. Adding a Dynamic User Control from the Base Page Class (and retaining it on Postback?)
      Hi There! As the subject says, I need to add a user control dynamically (using LoadControl function) to the derieved page from the base Page...
    3. Changing text to a textbox control in row 5 of a datagrid
      dim txt as textbox txt=datagrid1.item(0).controls(2) txt.text=xxxxxx "William Pulling via .NET 247" <anonymous@dotnet247.com>, haber iletisinde...
    4. Adding a dynamic textbox control to a datagrid with Datatable datasource
      Hi, I have a datagrid, and I want to bind this to a datatable, and I would like to have textbox controls and one button control. The problem...
    5. VERY STRANGE BUG? Adding a textbox control causes other textbox control to fail???
      I've had this happen a few times too. I'm still not positive of what exactly causes it but all that's happening is the control that no longer...
  3. #2

    Default Re: Accessing Textbox Text After Postback in Dynamic Control

    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 Guest

  4. #3

    Default Re: Accessing Textbox Text After Postback in Dynamic Control

    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 Guest

  5. #4

    Default Re: Accessing Textbox Text After Postback in Dynamic Control

    Elroy... I found this in google and used it with success

    [url]http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet.buildingc ontrols/browse_frm/thread/a59b8e00e37d1e91/3fddbe93a2d28066?lnk=st&q=isfirstload&rnum=1&hl=en #3fddbe93a2d28066[/url]

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

    [url]http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx[/url]

    hth

    Jose

    "Elroyskimms" <elroyskimms@yahoo.com> wrote in message
    news:1140463121.205414.238520@z14g2000cwz.googlegr oups.com...
    > 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139