Asp.Net RadioButtonList 选定的 ListItem 在页面重新访问时无法识别

发布于 2024-12-02 11:38:06 字数 1492 浏览 0 评论 0原文

在我的送货地址页面上有两个 RadioButtonList ListItems(代码如下所示),它们根据用户输入写入数据库 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;
        }
    }
}

On my shipping address page there are two RadioButtonList ListItems (code shown below) that write 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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如此安好 2024-12-09 11:38:06

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

您还应该将初始化代码包装在 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文