Skip to content

Instantly share code, notes, and snippets.

@jpoehls
Created October 5, 2009 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpoehls/201827 to your computer and use it in GitHub Desktop.
Save jpoehls/201827 to your computer and use it in GitHub Desktop.
ASP.NET DropDownList SelectedValue & ViewState
protected string MySelectedValue
{
get
{
return (ViewState["MySelectedValue"] == null)
? null : ViewState["MySelectedValue"].ToString();
}
set
{
ViewState["MySelectedValue"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
// Check if the selectedValue has changed.
string selectedValue = Request[myDropDownCtrl.UniqueID];
if (selectedValue != MySelectedValue)
{
// Value has changed, do whatever you
// would do in the SelectedIndexChanged event here.
}
MySelectedValue = selectedValue;
// TODO: Populate the drop-down control
PopulateMyDropDownCtrl();
// set the set the selectedValue on the drop-down
if (selectedValue != null)
{
myDropDownCtrl.Items.FindByValue(selectedValue).Selected = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment