【问题标题】:Incoherent postback behavior with radio button list selection单选按钮列表选择的不连贯回发行为
【发布时间】:2020-03-18 14:02:42
【问题描述】:

我有一个:

  • RadioButtonList 默认选择第一项。

  • 取消选择此选项的链接按钮。

  • LinkBut​​ton 显示 RadioButtonList 的索引

如果我单击取消选择,然后单击保存,则会显示第一项的索引 0。这种看似错误的行为是由于重新加载页面并根据默认设置进行选择的回发造成的。

但是:如果我现在选择 List 的 Item2 然后单击保存,我希望回发将 RadioButtonList 重置为其初始值时会出现相同的行为。但显示 List-Item2 的索引 1。

所以“未选择”在回发时被否决,而“已选择”则不是! 真的是这样吗?令人困惑且难以处理。

    <asp:RadioButtonList ID="RBL_SelectType" runat="server">
        <asp:ListItem Text="Choice1" Value="0" Selected="True" />
        <asp:ListItem Text="Choice2" Value="1" />
    </asp:RadioButtonList>

    <asp:LinkButton ID="LB_Reset" runat="server" OnClick="Unselect">unselect RBL</asp:LinkButton>
    <asp:LinkButton ID="LB_Save" runat="server" OnClick="Save">Save</asp:LinkButton>

    <asp:Label runat="Server" ID="Message" />

与:

    protected void Save(object sender, EventArgs e)
    {
        Message.Text = "Index = " + Convert.ToString(RBL_SelectType.SelectedIndex);
    }

    protected void Unselect(object sender, EventArgs e)
    {
        RBL_RBL_SelectType.SelectedIndex = -1;
    }

【问题讨论】:

  • 你有 Page_Load 事件吗?
  • 不,我为这次测试扯掉了所有东西

标签: c# asp.net postback radiobuttonlist


【解决方案1】:

这是我遇到的问题的解决方案,尽管它不能解释我眼中一直语无伦次的行为;我希望收到其他用户对此行为的更多反馈或确认。

所以,当您需要为 RadioButtonList 设置默认选择时,请勿在标记中执行此操作,因为它会在回发后丢失“未选择任何内容”选择,但请在 page_load 后面的代码中执行此操作。

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
       RBL_SelectType.SelectedIndex = 0;
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 2016-06-17
    • 2021-09-02
    • 2013-03-01
    相关资源
    最近更新 更多