How to store a control in Viewstate [Resolved]

Posted by Sharpcnet under C# on 2/19/2014 | Points: 10 | Views : 991 | Status : [Member] | Replies : 3
I'm trying to store a dropdownlist control in viewstate. Not the selected value, but the control.

protected void ddl_selectedindexchanged(object sender, EventArgs e)
{
ViewState["sender"] = (dropdownlist)sender;
}


That gives an error as dropdownlist is not serializable. So as someone suggested, I tried this

ViewState["sender"] = ddl.ID;


but when trying to retrieve it, the value of ddl is null.

dropdownlist ddl = (dropdownlist)findcontrol(ViewState["sender"].tostring());


what is the right way to do this.




Responses

Posted by: Sharpcnet on: 2/19/2014 [Member] Starter | Points: 25

Up
0
Down

Resolved
Thank you everyone. Someone helped me out with the solution. The dropdown was inside a panel, so it has to be:
ViewState["sender"] = ddlControl.ID;
DropDownList ddl = (DropDownList)(panel1.FindControl(ViewState["sender"]));


Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajesh_Kumar on: 2/19/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Use Session instead of Viewstate.
With ViewState,you can make Serializable attribute only in Class level.
Session["sender"] = (dropdownlist)sender;


Rejesh Kumar

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sravan661 on: 2/19/2014 [Member] Bronze | Points: 25

Up
0
Down
hi,
please verify

The EnableViewState property for the page is set to true.
The EnableViewState property for the control is set to true.
The ViewStateMode property for the control is set to Enabled or inherits the Enabled setting.

Revert if u have any issues

sravan

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response