0

有没有办法向 SelectedIndexChanged 函数发送额外的参数?

 <asp:RadioButtonList 
                    ID="rblMeetingPlace" 
                    SelectedValue = '<%# Bind("intMtgLoc") %>'
            *OnSelectedIndexChanged = "Validate('txtMeetPlaceOther')"*
                    runat="server" 
                    RepeatDirection="Horizontal"
 >
<asp:ListItem Value="1">Workshop</asp:ListItem>
<asp:ListItem Value="2">Service provider agency</asp:ListItem>
<asp:ListItem Value="3">Advocacy organization</asp:ListItem>
<asp:ListItem Value="4">Public Space</asp:ListItem>
<asp:ListItem Value="5">Other (specify): </asp:ListItem>
<asp:ListItem Value="" Text="" style="display: none" />
</asp:RadioButtonList>
<asp:TextBox ID="txtMeetPlaceOther" Text='<%# Bind("strMtgLocOth") %>' 
runat="server" />

我有几个单选按钮列表,我想在选择“其他”时启用文本框。我正在考虑发送文本框的 id 来启用它。

任何想法?

4

1 回答 1

1

您可以通过以下方式轻松做到这一点:

<asp:radiobuttonlist id="rbl1" runat="server" 
    RepeatDirection="Horizontal" 
    AutopostBack="true"
    SelectedValue='<%# Bind("intMtgLoc") %>' 
    OnselectedIndexChanged="rbl1_SelectedIndexChanged">
        <asp:ListItem Value="1">Workshop</asp:ListItem>
        <asp:ListItem Value="2">Service provider agency</asp:ListItem>
        <asp:ListItem Value="3">Advocacy organization</asp:ListItem>
        <asp:ListItem Value="4">Public Space</asp:ListItem>
        <asp:ListItem Value="5">Other (specify): </asp:ListItem>
        <asp:ListItem Value="" Text="" style="display:none" />
        </asp:radiobuttonlist>
<asp:textbox id="txtMeetPlaceOther" text='<%# Bind("strMtgLocOth") %>' runat="server" />
<asp:textbox id="TextBox1" enabled="false" runat="server"></asp:textbox>
<asp:textbox id="TextBox2" enabled="false" runat="server"></asp:textbox>

在代码隐藏中:

protected void rbl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        *yourValidatorName*.Validate();
        if (Convert.ToInt32(rbl1.SelectedValue) == 5)
        {
            TextBox1.Enabled = true;
            TextBox2.Enabled = true;
        }
        else
        {
            TextBox1.Enabled = false;
            TextBox2.Enabled = false;
        }
    }

回复评论:首先你应该OnSelectedIndexChanged为所有的RadioButtonLists事件处理程序设置。这里—— rbl_SelectedIndexChanged。然后在后面的代码中:

protected void rbl_SelectedIndexChanged(object sender, EventArgs e)
        {

            TextBox[] textboxes = new TextBox[] { TextBox1, TextBox2 };//all your textboxes.

            RadioButtonList whoCallEvent = sender as RadioButtonList;


            string last = whoCallEvent.ID.ToString().Substring(whoCallEvent.ID.ToString().Length - 1, 1);//get the last symbol of object (TextBox) ID,  who call event. 
            int index = Convert.ToInt32(last);
            if (Convert.ToInt32(whoCallEvent.SelectedValue) == 5)
            {
                textboxes[index - 1].Enabled = true;
            }
            else
            {
                textboxes[index - 1].Enabled = false;
            }
        }

但我认为这样做在概念上是错误的。最好的方法是为我页面上rbl_SelectedIndexChanged的所有内容创建。radioButtonList

于 2012-05-01T20:57:57.993 回答