【问题标题】:How to send extra parameter on SelectedIndexChanged?如何在 SelectedIndexChanged 上发送额外参数?
【发布时间】:2012-05-01 18:28:25
【问题描述】:

有什么方法可以向 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 来启用它。

有什么想法吗?

【问题讨论】:

    标签: asp.net parameter-passing selectedindexchanged extra


    【解决方案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;
            }
        }
    

    回复评论: 首先,您应该为所有RadioButtonLists 事件处理程序设置OnSelectedIndexChanged。在这里-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;
                }
            }
    

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

    【讨论】:

    • 使用您的方法,我必须为页面上的所有 radioButtonList 创建 rbl1_SelectedIndexChanged。我想创建一个可以处理 TextBox 启用/禁用的函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多