Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retreiving values!

Status
Not open for further replies.

McKaulick

Programmer
Oct 24, 2002
35
AR
Hi all! I dynammicaly add radiobutton on a webform! When I submit the form, I want to retreive all the value, but I can't get them! Is there a way to do that? Because I can't acess the controls after the submit!

I hope I'ts clear...

Mckaulick
 
The framework for WebForms does not save automatically dynamically added controls! This came as a surprise for me also...
The documentation is not very clear on this problem. The only advice you can get is to override the methods SaveViewState() and LoadViewState() for the form. I tried this with drop down lists, but it did not work too well, because they lost their associated events, properties and data sources. And the worst thing is that the two methods, that should take care of saving the control's state, are private, so you cannot call them directly from the form, but only by deriving from the control class (DropDownList in my case).
I have decided therefore to save the controls in the session, and this works, except for the fact that you still have to add the associated event handlers each time the form refreshes!
I know many people who have problems with this thing. I am curious if anybody found a better solution...
 
Thanks Alexboly for these informations, but I don't really understand what you say when you say that you save the controls in the Session. Do you think you might give me more informations about that? Sample code would be nice too!

It's kind of strange actually that you can't access values of dynamic controls 'cos I'm wondering why we can create them if after that we can't use them!

Anyway, I will continue searching!

Thanks!

McKaulick
 
It would be necessary for you to look in MSDN at the lifecycle for server controls topic (ms-help://MS.MSDNQTR.2002JAN.1033/cpguide/html/cpconcontrolexecutionlifecycle.htm).
What happens in fact is that each server control transforms into html. Its state is converted to a string that appears in the generated html page as a hidden field (this is what SaveViewState() does).
I think that what is not clear to you is that the html page that the user sees is generated each time it is posted to the server. The framework knows how to automatically regenerate the html code for the static controls, but it doesn't know how to do that for the dynamic controls. That is in fact what happens.
So, you will have to do the following things:
1) add the control to the page each time it is loaded
2) save the control state somehow each time before the page is sent again to the client.

I show you an idea of how this should work:

private Control dynControl;
private void Page_Load(object sender, System.EventArgs e)
{
...
if(Session["dynControl"] != null
{
dynControl = (Control)Session["dynControl"];
}
...
}

override protected void OnInit(EventArgs e)
{
....
Page.Controls.Add(dynControl);
....
}

private void Some_control_event()
{
...
Session["dynControl"] = dynControl;
...
}

It was rather strange for me too to find this out...
 
Thanks again, but I do find it weird that even If I can't have access to the controls, I should have somewhere access to the values of the controls!

Anyway, See ya later!

Patrice
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top