On my shipping address page there are two RadioButtonList ListItems (code shown below) that write 开发者_Go百科to a database true or false based on user input.
The first time a user selects a value and moves to the next step of the checkout process their ship to address type is stored correctly (true / false) in a database. If they go back to the shipping address page, select the opposite ListItem and move on to the next checkout page, their updated shipping type is not changed in the database. It is as if the ListItem does not recognize the user's radiobutton selection has changed when revisiting the page.
Can someone please help figure this one out?
ShippingAddress.ascx
<asp:RadioButtonList id="ShipToAddressType" runat="server">
<asp:ListItem Value="0" id="businessShipping">My shipping address is a business.</asp:ListItem>
<asp:ListItem Value="1" id="residenceShipping">My shipping address is a residence.</asp:ListItem>
</asp:RadioButtonList>
ShippingAddress.ascx.cs
if (residenceShipping.Selected == true)
shippingAddress.Residence = true;
else
shippingAddress.Residence = false;
ShippingAddress.ascx.cs Page_Load
protected void Page_Load(object sender, EventArgs e)
{
User user = Token.Instance.User;
Address shipAddress = null;
foreach (Address tempAddress in user.Addresses) if (tempAddress.Nickname == "Shipping") shipAddress = tempAddress;
// sets radio button of return users previously selected ship type
if (shipAddress != null)
{
if (shipAddress.Residence == false)
{
ShipToAddressType.SelectedIndex = 0;
}
else
{
ShipToAddressType.SelectedIndex = 1;
}
}
}
You need to move the code in Page_Load
to Page_Init
. Otherwise ViewState
won't work and you won't get change events. View state is loaded after Init
before PreLoad
.
You should also wrap your init code in a IsPostBack
check. Although I may misunderstand what you are doing here.
protected void Page_Init(EventArgs e)
{
if (!IsPostBack)
{
User user = Token.Instance.User;
Address shipAddress = null;
foreach (Address tempAddress in user.Addresses)
{
if (tempAddress.Nickname != "Shipping")
{
continue;
}
ShipToAddressType.SelectedIndex = 1;
}
}
ShipToAddressType.SelectedIndexChanged += ShipToAddressType_SelectedIndexChanged;
}
void ShipToAddressType_SelectedIndexChanged(object sender, EventArgs e)
{
// save the new state to database
// redirect to enforce refresh of saved state
Response.Redirect(Request.RawUrl);
}
精彩评论