i read few articles and i decided to disable view state on some of my aspx page controls like dropdown. i am having problem that after postback second dropdown forgets the value but first dropdown is working fine. please see the code below. how can i improve
it?
Where you have disabled the ViewState for dropdown, and as per your code your dropdown ddlGroup will bind on every post back hence its selected value will be reseted to 0th index
The one which is in UpdatePanel will retain its value on postback, however the one which is outside the update panel will not as the viewstate is false for the page
I believe on every ddlGroups_SelectedIndexChanged event your ddlSites is loosing its selected value? Is It?
If it is then it has bound to happen becouse on every ddlGroups_SelectedIndexChanged you are rebinding the ddlSites again hence it is loosing its selected value.
solideagle
Member
163 Points
124 Posts
dependent dropdowns viewstate disable problem
Feb 08, 2012 11:33 AM|LINK
hi guys,
i read few articles and i decided to disable view state on some of my aspx page controls like dropdown. i am having problem that after postback second dropdown forgets the value but first dropdown is working fine. please see the code below. how can i improve it?
<asp:DropDownList runat="server" ID="ddlGroup" AutoPostBack="true" DataTextField="Name" DataValueField="ID" OnSelectedIndexChanged="ddlGroups_SelectedIndexChanged" AppendDataBoundItems="true"> <asp:ListItem Text="--Select Group--" Value="0" /> </asp:DropDownList> </td> </tr> <tr> <td style="width: 35%"> <asp:Label Text="Site" runat="server" ClientIDMode="Static" /> </td> <td style="width: 65%"> <asp:UpdatePanel runat="server" UpdateMode="Conditional"> <Triggers> <asp:AsyncPostBackTrigger ControlID="ddlGroup" EventName="SelectedIndexChanged" /> </Triggers> <ContentTemplate> <asp:DropDownList runat="server" ID="ddlSite" DataTextField="Name" DataValueField="ID"> </asp:DropDownList> </ContentTemplate> </asp:UpdatePanel>protected override void OnInit(EventArgs e) { MasterPage = (Site)this.Master; BindddlGroup(); base.OnInit(e); } protected void Page_Load(object sender, EventArgs e) { } #endregion #region dropdown methods protected void BindddlGroup() { DataTable dt = this.API.Company.GetAllGroups(Utility.UserId(), MasterPage.IsAdmin); ddlGroup.DataSource = dt; ddlGroup.DataBind(); if (!MasterPage.IsAdmin) { ddlGroup.Items.RemoveAt(0); } ddlGroups_SelectedIndexChanged(null, null); } protected void ddlGroups_SelectedIndexChanged(object sender, EventArgs e) { DataTable dt = this.API.Company.GetGroupSite(Utility.UserId(), ddlGroup.SelectedValue, MasterPage.IsAdmin); ddlSite.DataSource = dt; ddlSite.DataBind(); if (!MasterPage.IsSiteManager) ddlSite.Items.Insert(0, new ListItem("--Select Site--", "0")); }Is there anything i can do or i have to use viewstate? I have a button on the page. which does not do anything except postback yet.
thanks
devensawant
Contributor
3763 Points
858 Posts
Re: dependent dropdowns viewstate disable problem
Feb 08, 2012 12:14 PM|LINK
Where you have disabled the ViewState for dropdown, and as per your code your dropdown ddlGroup will bind on every post back hence its selected value will be reseted to 0th index
viewstate state
If my post helps you then please mark as answer
solideagle
Member
163 Points
124 Posts
Re: dependent dropdowns viewstate disable problem
Feb 08, 2012 12:36 PM|LINK
hi deven,
Viewstate has been disabled on page directive. according to below article you should bind it on init event and it will have same behaviour.
http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx
but other dropdown is not working. and i have a button on the page. which does not do anything except postback yet.
viewstate state
devensawant
Contributor
3763 Points
858 Posts
Re: dependent dropdowns viewstate disable problem
Feb 08, 2012 12:42 PM|LINK
Which dropdown is retaining value?
The one which is in UpdatePanel will retain its value on postback, however the one which is outside the update panel will not as the viewstate is false for the page
viewstate state
If my post helps you then please mark as answer
solideagle
Member
163 Points
124 Posts
Re: dependent dropdowns viewstate disable problem
Feb 08, 2012 12:46 PM|LINK
quite the opposite. the dropdown ddlGroup is retaining the value. but ddlSite is not retaining the value when clicked on the button. any idea?
PS: i have checked by enabling viewstate on ddlsite but it still does not remember the values...thats strange.
devensawant
Contributor
3763 Points
858 Posts
Re: dependent dropdowns viewstate disable problem
Feb 09, 2012 04:53 AM|LINK
Give a try to
protected override void OnInit(EventArgs e) { MasterPage = (Site)this.Master; if(!IsPostBack) { BindddlGroup(); } base.OnInit(e); }If my post helps you then please mark as answer
solideagle
Member
163 Points
124 Posts
Re: dependent dropdowns viewstate disable problem
Feb 09, 2012 07:45 AM|LINK
Nope does not solve it. any other idea.
devensawant
Contributor
3763 Points
858 Posts
Re: dependent dropdowns viewstate disable problem
Feb 09, 2012 08:09 AM|LINK
I believe on every ddlGroups_SelectedIndexChanged event your ddlSites is loosing its selected value? Is It?
If it is then it has bound to happen becouse on every ddlGroups_SelectedIndexChanged you are rebinding the ddlSites again hence it is loosing its selected value.
Correct me if I am mistaking.
If my post helps you then please mark as answer
solideagle
Member
163 Points
124 Posts
Re: dependent dropdowns viewstate disable problem
Feb 09, 2012 09:59 AM|LINK
Yes exactly but i am not sure how to improve the code to remove this...working without viewstate is another hella problem.