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
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("t xtEditPlan ningManCom ments"), TextBox)
Dim txt2 As TextBox = CType(e.Row.FindControl("t xtEditComp etencyDesc ription"), 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="txtEditCompetencyDescr iption" CssClass="inputtext2" TextMode="MultiLine" BorderStyle="Solid" BorderColor="Silver" BorderWidth="1px" Width="99%" Height="200px" runat="server" Text='<%# Bind("CompetencyDescriptio n") %>'></asp:TextBox>
<asp:TextBox ID="txtEditPlanningManComm ents" CssClass="inputtext2" TextMode="MultiLine" BorderStyle="Solid" BorderColor="Silver" BorderWidth="1px" Width="99%" Height="200px" runat="server" Text='<%# Bind("ManagersComments") %>'></asp:TextBox>
</EditItemTemplate>
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("t
Dim txt2 As TextBox = CType(e.Row.FindControl("t
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="txtEditCompetencyDescr
<asp:TextBox ID="txtEditPlanningManComm
</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("t xtEditPlan ningManCom ments"), TextBox)
Dim txt2 As TextBox = CType(e.Row.FindControl("t xtEditComp etencyDesc ription"), TextBox)
If (txt1.Text = "No Comments Yet!!") Then
txt2.Visible = False
Else
txt2.Visible = True
End If
End If
If (e.Row.RowType = DataControlRowType.DataRow
Dim txt1 As TextBox = CType(e.Row.FindControl("t
Dim txt2 As TextBox = CType(e.Row.FindControl("t
If (txt1.Text = "No Comments Yet!!") Then
txt2.Visible = False
Else
txt2.Visible = True
End If
End If
ASKER
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
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?
ASKER
Hi, sorry about that.
here is the code
Protected Sub PlanningGridView_RowDataBo und(ByVal sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs ) Handles PlanningGridView.RowDataBo und
If e.Row.RowType = DataControlRowType.DataRow Then
Dim txt1 As TextBox = CType(e.Row.FindControl("t xtEditPlan ningManCom ments"), TextBox)
Dim txt2 As TextBox = CType(e.Row.FindControl("t xtEditComp etencyDesc ription"), 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
here is the code
Protected Sub PlanningGridView_RowDataBo
If e.Row.RowType = DataControlRowType.DataRow
Dim txt1 As TextBox = CType(e.Row.FindControl("t
Dim txt2 As TextBox = CType(e.Row.FindControl("t
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
ASKER
Hi, i have just created a little project with the relevant items, i'll attach the code
Mick
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks very much for that, it now works fine.
Thanks again
Mick
Thanks again
Mick
Dim txt1 As TextBox = CType(e.Row.FindControl("T
Dim txt2 As TextBox = CType(e.Row.FindControl("T
If (txt1.Text = "HideTextBox2") Then
txt2.Visible = False
Else
txt2.Visible = True
End If