根据gridview中的下拉列表值设置文本框可见性

时间:2010-08-27 18:53:31

标签: asp.net vb.net gridview drop-down-menu selectedindexchanged

我有一个gridview,我想根据同一行下拉列表的选定值显示或隐藏文本框。

我的网格视图:

                <asp:GridView ID="GridViewUsers" runat="server" AutoGenerateColumns="False" CssClass="TableFramed">
                <Columns>
                    <asp:TemplateField HeaderText="Type">
                        <ItemTemplate>
                            <asp:DropDownList ID="ddlType" runat="server" AutoPostBack="true">
                                <asp:ListItem Value="1">Overtime</asp:ListItem>
                                <asp:ListItem Value="2">Temporary</asp:ListItem>
                                <asp:ListItem Value="3">Permanent</asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>                        
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="First Name">
                        <ItemTemplate>
                            <asp:TextBox ID="txtFName" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="FNameValidator" runat="server" Text="*" ControlToValidate="txtFName" Display="Dynamic"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                    <asp:TemplateField HeaderText="hide me">
                        <ItemTemplate>
                            <asp:TextBox ID="txtHideMe" runat="server"></asp:TextBox>
                        </ItemTemplate>
                </Columns>
             </asp:gridview>

如何连接它以便可以根据下拉列表的选定值隐藏或显示txtHideMe文本框?

Codebehide:

   Protected Sub ddlType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For Each row In GridViewUsers.Rows
            Dim reqType As DropDownList = CType(row.FindControl("ddlType"), DropDownList)
            Dim txtHideMeAs TextBox = CType(row.FindControl("txtHideMe"), TextBox)

            If reqType.SelectedItem.Value = "2" Then
                txtHideMe.Visible = "False"
            End If
        Next
    End Sub

编辑: 我还希望能够在文本框中使用必需的字段验证器,如果它没有隐藏的话。

1 个答案:

答案 0 :(得分:0)

看起来您需要将SelectedIndexChanged事件绑定到控件:

<asp:DropDownList runat="server" ID="ddlType" OnSelectedIndexChanged="ddlType_SelectedIndexChanged" ...>