Accessing Textbox Text After Postback in Dynamic Control

E

Elroyskimms

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?
 
E

Elroyskimms

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?
 
E

Elroyskimms

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!
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top