如何在SelectedIndexChanged上发送额外参数?

时间:2012-05-01 18:28:25

标签: asp.net parameter-passing selectedindexchanged extra

有没有办法向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" />

我有几个radiobuttonlist,我想在选择“其他”时启用文本框。 我正在考虑发送文本框的ID以启用它。

有什么想法吗?

1 个答案:

答案 0 :(得分: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