ASP.NET DropDownLost loses values on PostBack

  • Thread starter Thread starter Roger R. Smith
  • Start date Start date
R

Roger R. Smith

Hi all, I am sure you have seen this issue. I have an aspx page which loads
a UserControl at runtime.
In this user control I have a dropdownlist which is populated dynamically.
If I click a submit button and try to put the selectedvalue from the
dropdown into the session, it will always put the first selected item into
the session, not the actual item I selected.
Does anyone have any advice for this?
 
Sounds like you have something like this...

Sub page_load
BindDropDownList()
End Sub

It needs to be this

Sub page_load
if not page.ispostback()
BindDropDownList()
end if
end sub
 
Thank you Raterus, I do have that check in place.
here is the code:

public class Reseller : CheckoutModule

{

protected System.Web.UI.WebControls.DropDownList drpReseller;

protected ServicesController servicesCtrl = new ServicesController();

protected System.Web.UI.WebControls.CheckBoxList licenseKeyList;

private void Page_Load(object sender, System.EventArgs e)

{

if(!Page.IsPostBack)

{

if(Request.Params["ProductID"] != null)

{

ViewState["PRODUCT_ID"] = Request.Params["ProductID"];

BindLicenseKeyResellers();

BindResellers();

}

}

}

private void BindLicenseKeyResellers()

{

licenseKeyList.DataSource =
servicesCtrl.GetLicenseKeyResellersByProductID(Convert.ToInt32(ViewState["PR
ODUCT_ID"]));

licenseKeyList.DataBind();

}

private void BindResellers()

{

drpReseller.DataSource =
servicesCtrl.GetLicenseKeyResellersByProductID(Convert.ToInt32(ViewState["PR
ODUCT_ID"]));

drpReseller.DataBind();

}

public override void SaveVarsToSession()

{

Session["RESELLER_KEY"] = drpReseller.SelectedIndex;

}



Sounds like you have something like this...

Sub page_load
BindDropDownList()
End Sub

It needs to be this

Sub page_load
if not page.ispostback()
BindDropDownList()
end if
end sub
 
We can't tell the structure of your data source from this code. Make sure
the value of each item in the dropdown is different, since that is what
actually gets posted (not the index of the selected item). If they are all
the same, then the first one will always look like it is selected on the
server.

Roger R. Smith said:
Thank you Raterus, I do have that check in place.
here is the code:

public class Reseller : CheckoutModule

{

protected System.Web.UI.WebControls.DropDownList drpReseller;

protected ServicesController servicesCtrl = new ServicesController();

protected System.Web.UI.WebControls.CheckBoxList licenseKeyList;

private void Page_Load(object sender, System.EventArgs e)

{

if(!Page.IsPostBack)

{

if(Request.Params["ProductID"] != null)

{

ViewState["PRODUCT_ID"] = Request.Params["ProductID"];

BindLicenseKeyResellers();

BindResellers();

}

}

}

private void BindLicenseKeyResellers()

{

licenseKeyList.DataSource =
servicesCtrl.GetLicenseKeyResellersByProductID(Convert.ToInt32(ViewState["PR
ODUCT_ID"]));

licenseKeyList.DataBind();

}

private void BindResellers()

{

drpReseller.DataSource =
servicesCtrl.GetLicenseKeyResellersByProductID(Convert.ToInt32(ViewState["PR
ODUCT_ID"]));

drpReseller.DataBind();

}

public override void SaveVarsToSession()

{

Session["RESELLER_KEY"] = drpReseller.SelectedIndex;

}



Sounds like you have something like this...

Sub page_load
BindDropDownList()
End Sub

It needs to be this

Sub page_load
if not page.ispostback()
BindDropDownList()
end if
end sub

Roger R. Smith said:
Hi all, I am sure you have seen this issue. I have an aspx page which loads
a UserControl at runtime.
In this user control I have a dropdownlist which is populated dynamically.
If I click a submit button and try to put the selectedvalue from the
dropdown into the session, it will always put the first selected item into
the session, not the actual item I selected.
Does anyone have any advice for this?
 
Sorry here is the ascx code:

<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="Reseller.ascx.cs"
Inherits="StrikeIronWebServicePortal.StrikeIronPortalWebApp.PortalUser.Modul
es.Reseller" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<table>
<tr><td>License Key Compatibility:</td><td><asp:CheckBoxList
ID="licenseKeyList" Runat=server DataTextField="ResellerName"
DataValueField="ResellerKey" EnableViewState=True/></td></tr>
<tr>
<td>
Reseller:
</td>
<td>
<asp:DropDownList id="drpReseller" runat="server"
DataTextField="ResellerName"
DataValueField="ResellerKey"></asp:DropDownList>
</td>
</tr>
</table>


ResellerKey and ResellerName are both unique.
Thanks,
rog



Marina said:
We can't tell the structure of your data source from this code. Make sure
the value of each item in the dropdown is different, since that is what
actually gets posted (not the index of the selected item). If they are all
the same, then the first one will always look like it is selected on the
server.

Roger R. Smith said:
Thank you Raterus, I do have that check in place.
here is the code:

public class Reseller : CheckoutModule

{

protected System.Web.UI.WebControls.DropDownList drpReseller;

protected ServicesController servicesCtrl = new ServicesController();

protected System.Web.UI.WebControls.CheckBoxList licenseKeyList;

private void Page_Load(object sender, System.EventArgs e)

{

if(!Page.IsPostBack)

{

if(Request.Params["ProductID"] != null)

{

ViewState["PRODUCT_ID"] = Request.Params["ProductID"];

BindLicenseKeyResellers();

BindResellers();

}

}

}

private void BindLicenseKeyResellers()

{

licenseKeyList.DataSource =
servicesCtrl.GetLicenseKeyResellersByProductID(Convert.ToInt32(ViewState["PR
ODUCT_ID"]));

licenseKeyList.DataBind();

}

private void BindResellers()

{

drpReseller.DataSource =
servicesCtrl.GetLicenseKeyResellersByProductID(Convert.ToInt32(ViewState["PR
ODUCT_ID"]));

drpReseller.DataBind();

}

public override void SaveVarsToSession()

{

Session["RESELLER_KEY"] = drpReseller.SelectedIndex;

}



Sounds like you have something like this...

Sub page_load
BindDropDownList()
End Sub

It needs to be this

Sub page_load
if not page.ispostback()
BindDropDownList()
end if
end sub

Roger R. Smith said:
Hi all, I am sure you have seen this issue. I have an aspx page which loads
a UserControl at runtime.
In this user control I have a dropdownlist which is populated dynamically.
If I click a submit button and try to put the selectedvalue from the
dropdown into the session, it will always put the first selected item into
the session, not the actual item I selected.
Does anyone have any advice for this?
 
Back
Top