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

发布于 2024-09-16 10:47:35 字数 2272 浏览 16 评论 0原文

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

我的 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 文本框?

代码隐藏:

   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

编辑: 如果文本框未隐藏,我还希望能够在文本框上使用必填字段验证器。

I have a gridview that i would like to show or hide a text box based on the selected value of a dropdownlist on the same row.

My 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>

How do i wire it up so that the txtHideMe textbox can be hidden or displayed based off of the selected value of the dropdownlist?

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

Edit:
I would also like to be able to use a required field validator on the textbox if it's not hidden.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白首有我共你 2024-09-23 10:47:35

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

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

It looks like you need to bind the SelectedIndexChanged event to the control:

<asp:DropDownList runat="server" ID="ddlType" OnSelectedIndexChanged="ddlType_SelectedIndexChanged" ...>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文