asp.net building controls:
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?
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?
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!
Elroy... I found this in google and used it with success http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet.buildingcontrols/browse_frm/thread/a59b8e00e37d1e91/3fddbe93a2d28066?lnk=st&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 [quoted text, click to view] "Elroyskimms" <elroyskimms@yahoo.com> wrote in message news:1140463121.205414.238520@z14g2000cwz.googlegroups.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! >
Don't see what you're looking for? Try a search.
|