根据 gridview 中的下拉列表值设置文本框可见性
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您需要将 SelectedIndexChanged 事件绑定到控件:
It looks like you need to bind the SelectedIndexChanged event to the control: