Link to home
Start Free TrialLog in
Avatar of Mick_Buckley
Mick_Buckley

asked on

hide a textbox in a gridview template depending on the value in another textbox

I have a asp.net gridview (vb) and when i click the edit button i would like one of the textboxes to have its visibility depend upon the value of another textbox
Avatar of prairiedog
prairiedog
Flag of United States of America image

Use GridView's RowDataBound event handler:
Dim txt1 As TextBox = CType(e.Row.FindControl("TextBox1"), TextBox)
Dim txt2 As TextBox = CType(e.Row.FindControl("TextBox2"), TextBox)
If (txt1.Text = "HideTextBox2") Then
     txt2.Visible = False
Else
   txt2.Visible = True
End If
Avatar of Mick_Buckley
Mick_Buckley

ASKER

Hi, when i run your code i get the following error message
Object reference not set to an instance of an object.
and the following line is highlighted
If (txt1.Text = "No Comments Yet!") Then

here is the code
Dim txt1 As TextBox = CType(e.Row.FindControl("txtEditPlanningManComments"), TextBox)
Dim txt2 As TextBox = CType(e.Row.FindControl("txtEditCompetencyDescription"), TextBox)
  If (txt1.Text = "No Comments Yet!!") Then
    txt2.Visible = False
  Else
    txt2.Visible = True
End If

The textboxes in question are in the edititem template of my grid view
<EditItemTemplate>
  <asp:TextBox ID="txtEditCompetencyDescription" CssClass="inputtext2" TextMode="MultiLine"   BorderStyle="Solid" BorderColor="Silver" BorderWidth="1px" Width="99%" Height="200px" runat="server" Text='<%# Bind("CompetencyDescription") %>'></asp:TextBox>
<asp:TextBox ID="txtEditPlanningManComments" CssClass="inputtext2" TextMode="MultiLine" BorderStyle="Solid" BorderColor="Silver" BorderWidth="1px" Width="99%" Height="200px" runat="server" Text='<%# Bind("ManagersComments") %>'></asp:TextBox>
</EditItemTemplate>
In RowDataBound event handler, you will need to check if a row is a DataRow before you can use e.Row.FindControl.

If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim txt1 As TextBox = CType(e.Row.FindControl("txtEditPlanningManComments"), TextBox)
Dim txt2 As TextBox = CType(e.Row.FindControl("txtEditCompetencyDescription"), TextBox)
  If (txt1.Text = "No Comments Yet!!") Then
    txt2.Visible = False
  Else
    txt2.Visible = True
End If
End If

Hi prairiedog,
I get exactly the same error on the same line using you revised code.

This happens when the page loads, before i click the edit button.

Thanks for your help so far
Can you post your code now?
Hi, sorry about that.

here is the code
 Protected Sub PlanningGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles PlanningGridView.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim txt1 As TextBox = CType(e.Row.FindControl("txtEditPlanningManComments"), TextBox)
            Dim txt2 As TextBox = CType(e.Row.FindControl("txtEditCompetencyDescription"), TextBox)
            If (txt1.Text = "No Comments Yet!!") Then
                txt2.Visible= False
            Else
                txt2.Visible= True
            End If
        End If
    End Sub

The edit template code is as in the previous code, I've not changed it.

Thanks again
Mick
Hi, i have just created a little project with the relevant items, i'll attach the code

Mick
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PerformanceManagementConnectionString %>"
            SelectCommand="SELECT [id], [objectivedescription], [managercomments] FROM [objectives]"></asp:SqlDataSource>
    
    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                    SortExpression="id" Visible="False" />
                <asp:TemplateField HeaderText="objectivedescription" SortExpression="objectivedescription">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtobjectivesdescription" runat="server" Text='<%# Bind("objectivedescription") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("objectivedescription") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="managercomments" SortExpression="managercomments">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtmanagercomments" runat="server" Text='<%# Bind("managercomments") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("managercomments") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </form>
</body>
</html>
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
 
            Dim txt1 As TextBox = CType(e.Row.FindControl("txtmanagercomments"), TextBox)
            Dim txt2 As TextBox = CType(e.Row.FindControl("txtobjectivesdescription"), TextBox)
            If (txt1.Text = "There are no comments") Then
                txt2.Visible = False
            Else
                txt2.Visible = True
            End If
        End If
    End Sub
 
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks very much for that, it now works fine.
Thanks again
Mick