Web User Control - Dropdownlist Postback Dataloss
Hi,
I want to use a Dropdownlist on a Web User Control (ascx). The problem is when I set the Dropdownlist items manually on postback the items are still their and the onSelectedItemsChange event fires.
i.e.
<
asp:DropDownList
ID
=
"DropDownList1"
runat
=
"server"
onselectedindexchanged
=
"DropDownList1_SelectedIndexChanged"
AutoPostBack
=
"True"
>
<
asp:ListItem
Value
=
"1"
Text
=
"eins"
></
asp:ListItem
>
<
asp:ListItem
Value
=
"2"
Text
=
"zwei"
></
asp:ListItem
>
</
asp:DropDownList
>
protected
void
Page_Load(
object
sender, EventArgs e)
//comes here
protected
void
DropDownList1_SelectedIndexChanged(
object
sender, EventArgs e)
//comes here and got the right index
<
asp:DropDownList
ID
=
"DropDownList1"
runat
=
"server"
onselectedindexchanged
=
"DropDownList1_SelectedIndexChanged"
AutoPostBack
=
"True"
>
</
asp:DropDownList
>
protected
void
Page_Load(
object
sender, EventArgs e)
if
(!IsPostBack)
for
(
int
i = 0; i < 5; i++)
DropDownList1.Items.Add(
new
ListItem(i +
"name"
, i +
"value"
));
protected
void
DropDownList1_SelectedIndexChanged(
object
sender, EventArgs e)
//never end up here!
Hi Sydney,
You should enable the ViewState property on the Page where your UserControl lives. You can do this from the 'Title & Properties' option inside the Sitefinity backend.
Regards,
Daniel
Thanks. Thats what I needed. Still new in sitefinity *sight*
ARGH!!! This was bugging me for hours. All the ASP.Net sites where giving me all manner of suggestions from changing viewstate, object properties and other tricks. All to find it was Sitefinity being painful, because I didn't know about that enable viewstate property on the admin page.
Live and learn, but man... I've got a headache now. :(
Hi,
Come controls might require enable view state. To set is you will need to visit the "Titles and Properties" section of the page where the grid resides. There, under Advanced, you will see an unchecked box for Enable ViewState. I have included a screenshot for your convenience.
Usually, you can enableViewState for the whole site, for the page, or for your control. The same works in Sitefinity as well. You can enable ViewState for your page only, and set EnableViewState to false for all control that don't need it (set it to true for your control only). This is the only way you can partly enableViewState. Otherwise, even if you disable ViewState on page level and enable it on control level, it won't work, because control inherit the viewState of the page.