2011-09-02 65 views
0

在我的收货地址页面上有两个RadioButtonList ListItems(下面显示的代码)根据用户输入写入数据库true或false。Asp.Net RadioButtonList选择的ListItem页面上不能识别

用户第一次选择一个值并转到结帐过程的下一步时,他们的发货地址类型会正确存储在数据库中(true/false)。如果他们返回送货地址页面,请选择相反的ListItem并转到下一个结账页面,其更新的送货类型在数据库中不会更改。就好像ListItem不能识别用户的单选按钮选择在重新访问页面时已经改变。

有人可以帮忙解决这个问题吗?

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; 
     } 
    } 
} 
+0

该问题表明与页面生命周期有关的误解/错误。与给定的信息不能帮助! – humblelistener

+0

可能需要更多信息...您的代码隐藏中的PageLoad事件处理程序是什么样子的? – Brian

+0

我编辑帖子,添加上面的页面加载事件处理程序。谢谢! – Joe

回答

1

您需要将Page_Load中的代码移至Page_Init。否则ViewState将不起作用,您不会收到更改事件。视图状态在PreLoad之前的Init之后加载。

您还应该将您的init代码包装在IsPostBack检查中。尽管我可能会误解你在这里做什么。

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); 
} 
+0

装配手柄CodeKing。感谢您对我的头痛目标的快速反应和正确答案。 – Joe

相关问题