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
Mick_BuckleyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prairiedogCommented:
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
0
Mick_BuckleyAuthor Commented:
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>
0
prairiedogCommented:
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

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Mick_BuckleyAuthor Commented:
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
0
prairiedogCommented:
Can you post your code now?
0
Mick_BuckleyAuthor Commented:
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
0
Mick_BuckleyAuthor Commented:
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

0
prairiedogCommented:
Try adding one more check before you call FindControl:
 

If e.Row.RowType = DataControlRowType.DataRow Then
	If (e.Row.RowIndex = Me.GridView1.EditIndex) 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 If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mick_BuckleyAuthor Commented:
Thanks very much for that, it now works fine.
Thanks again
Mick
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.