为什么我的单选按钮列表仅在所选值= 1时触发

时间:2012-12-30 07:48:25

标签: c# asp.net ajax updatepanel radiobuttonlist

我不知道为什么只有在我选择第二个列表项时我的radiobuttonlist才会触发!

我的aspx:

    <asp:Panel ID="pnl_select_sign" runat="server" Visible="false">
            <asp:RadioButtonList ID="rb_select_sign" runat="server" AutoPostBack="true" RepeatDirection="Horizontal"
                OnSelectedIndexChanged="rb_select_sign_SelectedIndexChanged" CausesValidation="false" AppendDataBoundItems="true">
                <asp:ListItem Selected="True" Value="0">normal</asp:ListItem>
                <asp:ListItem Value="1">abnormal</asp:ListItem>
            </asp:RadioButtonList>
    </asp:Panel> 
  <div class="events" dir="rtl">
        <fieldset>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:Panel ID="pnl_PageNew_UC" runat="server" Width="100%">
                    </asp:Panel>
                    <asp:Panel ID="pnl_sign" runat="server" Width="100%" Visible="false">
                    </asp:Panel>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="rb_select_sign" EventName="SelectedIndexChanged" />
                </Triggers>
            </asp:UpdatePanel>
        </fieldset>
    </div>

我的.cs:

 protected void rb_select_sign_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rb_select_sign.SelectedValue == "0")
            {
                pnl_PageNew_UC.Visible = true;
                pnl_sign.Visible = false;

            }
            else
            {
                pnl_PageNew_UC.Visible = false;
                pnl_sign.Visible = true;

            }
        }

2 个答案:

答案 0 :(得分:1)

试试这段代码:

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Panel ID="pnl_select_sign" runat="server">
                <asp:RadioButtonList ID="rb_select_sign" runat="server" AutoPostBack="true" RepeatDirection="Horizontal"
                    OnSelectedIndexChanged="rb_select_sign_SelectedIndexChanged" CausesValidation="false"
                    AppendDataBoundItems="true">
                    <asp:ListItem Selected="True" Value="0">normal</asp:ListItem>
                    <asp:ListItem Value="1">abnormal</asp:ListItem>
                </asp:RadioButtonList>
            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>

    <div class="events" dir="rtl">
        <fieldset>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Panel ID="pnl_PageNew_UC" runat="server" Width="100%">
                        123
                    </asp:Panel>
                    <asp:Panel ID="pnl_sign" runat="server" Width="100%" Visible="false">
                        312
                    </asp:Panel>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="rb_select_sign" EventName="SelectedIndexChanged" />
                </Triggers>
            </asp:UpdatePanel>
        </fieldset>
    </div>

在代码背后:

        protected void rb_select_sign_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rb_select_sign.SelectedValue == "0")
            {
                pnl_PageNew_UC.Visible = true;
                pnl_sign.Visible = false;

            }
            else
            {
                pnl_PageNew_UC.Visible = false;
                pnl_sign.Visible = true;

            }

            UpdatePanel1.Update();
        }

答案 1 :(得分:1)

好吧,你的问题不是在selectedvalue =1时启动,只要你将面板的可见性设置为true就会启动。

问题本身位于AsyncPostBackTrigger,您的触发器 UpdatePanel之外,因此不再触发。

这很容易解决,将RadioButtonList放在ContentTemplate中:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
    <asp:Panel ID="pnl_select_sign" runat="server" >
        <asp:RadioButtonList ID="rb_select_sign" runat="server" AutoPostBack="true" RepeatDirection="Horizontal" ... />
    </asp:Panel> 
    <asp:Panel ID="pnl_PageNew_UC" runat="server" Width="100%">
    </asp:Panel>
....