?
Solved

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

Posted on 2008-11-14
9
Medium Priority
?
839 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:Mick_Buckley
  • 5
  • 4
9 Comments
 
LVL 22

Expert Comment

by:prairiedog
ID: 22964424
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
 

Author Comment

by:Mick_Buckley
ID: 22966412
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
 
LVL 22

Expert Comment

by:prairiedog
ID: 22968244
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Mick_Buckley
ID: 22968313
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
 
LVL 22

Expert Comment

by:prairiedog
ID: 22970973
Can you post your code now?
0
 

Author Comment

by:Mick_Buckley
ID: 22971086
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
 

Author Comment

by:Mick_Buckley
ID: 22971656
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
 
LVL 22

Accepted Solution

by:
prairiedog earned 1000 total points
ID: 22976236
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
 

Author Closing Comment

by:Mick_Buckley
ID: 31516960
Thanks very much for that, it now works fine.
Thanks again
Mick
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question