views:

106

answers:

1

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.

A: 

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

<asp:DropDownList runat="server" ID="ddlType" OnSelectedIndexChanged="ddlType_SelectedIndexChanged" ...>
Sam Huggill
@Sam - Thanks. I knew I was missing something but didn't think it was that simple.
zeroef