7 Replies - 11675 Views - Last Post: 22 December 2009 - 02:01 PM Rate Topic: -----

#1 jpcsmit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 26-September 09

Finding control inside a template field using button field

Posted 15 December 2009 - 01:08 PM

I have a button field that would make the panel inside a template field on the same row visible upon user click. I'm having problem seeing the panel control.

The panel control can be seen when the code is in rowdatabound but not when the code is in rowcommand event. I already tried storing the e As System.Web.UI.WebControls.GridViewRowEventArgs in a global variable but when the code is in the rowcommand part, the value of the global variable is now 'Nothing'. Please help. Thanks!

Private Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
		If e.CommandName = "show" Then
			Dim pan2 As Panel = DirectCast(a.Row.FindControl("Panel1"), Panel)
			pan.Visible = True
		End If
End Sub

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
		Dim pan as panel
		If e.Row.RowType = DataControlRowType.DataRow Then
			  pan = DirectCast(e.Row.FindControl("Panel1"), Panel)
		End If



Is This A Good Question/Topic? 0
  • +

Replies To: Finding control inside a template field using button field

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Finding control inside a template field using button field

Posted 16 December 2009 - 06:40 PM

You can't use 'e' because your in the RowCommand event and this is a GridViewCommandEventArgs object, not a GridViewRowEventArgs as in the RowDataBound event.

One way to get a reference to the row is to set the CommandArgument of the Button to be the index of the row. This way you can easily tell which row of the GridView you need to show the panel in.

The first thing you need to do is assign the index of the row when it is created to the CommandArgument property.

You can do this in the source of the page very easily and it will automatically be set when the Row is DataBound. I left out all the properties except the one I am illustrating, so don't be alarmed. :)

Add this CommandArgument="<%# Container.DataItemIndex %>" to your button.
<asp:Button CommandArgument="<%# Container.DataItemIndex %>" />


Once the index is set then you can easily grab the correct row and get a reference to it, so you can find the Panel.

Private Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
		If e.CommandName = "show" Then
			Dim index As Integer = Convert.ToInt32(e.CommandArgument)
			Dim pan2 As Panel = DirectCast(GridView1.Rows(index).FindControl("Panel1"), Panel)
			pan.Visible = True
		End If
End Sub


Was This Post Helpful? 0
  • +
  • -

#3 jpcsmit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 26-September 09

Re: Finding control inside a template field using button field

Posted 17 December 2009 - 10:10 AM

View PostJayman, on 16 Dec, 2009 - 05:40 PM, said:

You can't use 'e' because your in the RowCommand event and this is a GridViewCommandEventArgs object, not a GridViewRowEventArgs as in the RowDataBound event.

One way to get a reference to the row is to set the CommandArgument of the Button to be the index of the row. This way you can easily tell which row of the GridView you need to show the panel in.

The first thing you need to do is assign the index of the row when it is created to the CommandArgument property.

You can do this in the source of the page very easily and it will automatically be set when the Row is DataBound. I left out all the properties except the one I am illustrating, so don't be alarmed. :)

Add this CommandArgument="<%# Container.DataItemIndex %>" to your button.
<asp:Button CommandArgument="<%# Container.DataItemIndex %>" />


Once the index is set then you can easily grab the correct row and get a reference to it, so you can find the Panel.

Private Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
		If e.CommandName = "show" Then
			Dim index As Integer = Convert.ToInt32(e.CommandArgument)
			Dim pan2 As Panel = DirectCast(GridView1.Rows(index).FindControl("Panel1"), Panel)
			pan.Visible = True
		End If
End Sub



May I know where do i need to put the command argument code? here's my latest source:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="hijackthis._Default" MasterPageFile="~/Site1.Master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
		
	<center><asp:FileUpload ID="FileUpload1" runat="server" /></center>
	<br />
	<center><asp:TextBox ID="TextBox1" runat="server" Height="146px" Width="404px"></asp:TextBox></center>
	<br />
	<center><asp:Button ID="btnAnalyze" runat="server" Text="Analyze" />
		<br />
		<br />
	</center>
	<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
		GridLines="None" AutoGenerateColumns="False" DataKeyNames="entry" 
		DataSourceID="SqlDataSource1">
		<RowStyle BackColor="#E3EAEB" />
		<Columns>
			<asp:ButtonField ButtonType="Image" CommandName="show" 
				ImageUrl="~/pics/discuss.gif" Text="Button">
				<ItemStyle VerticalAlign="Top" />
			</asp:ButtonField>
			<asp:TemplateField HeaderText="Entry" SortExpression="entry">
				<EditItemTemplate>
					<asp:Label ID="Label3" runat="server" Text='<%# Eval("entry") %>'></asp:Label>
					<br />
					<br />
					<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
						ShowHeader="False">
						<Columns>
							<asp:BoundField DataField="Entry" HeaderText="Entry" SortExpression="Entry" 
								Visible="False" />
							<asp:BoundField DataField="Comment" HeaderText="Comment" 
								SortExpression="Comment" />
							<asp:BoundField DataField="Rating" HeaderText="Rating" 
								SortExpression="Rating" />
							<asp:BoundField DataField="Whois" HeaderText="Whois" SortExpression="Whois" />
						</Columns>
					</asp:GridView>
				</EditItemTemplate>
				<ItemTemplate>
					<asp:Label ID="Label3" runat="server" Text='<%# Bind("entry") %>'></asp:Label>
					<br />
					<asp:Panel ID="Panel1" runat="server">
						<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
							ShowHeader="False">
							<Columns>
								<asp:BoundField DataField="Entry" HeaderText="Entry" SortExpression="Entry" />
								<asp:BoundField DataField="Comment" HeaderText="Comment" 
									SortExpression="Comment" />
								<asp:TemplateField HeaderText="Rating" SortExpression="Rating">
									<EditItemTemplate>
										<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Rating") %>'></asp:TextBox>
									</EditItemTemplate>
									<ItemTemplate>
										<asp:Label ID="Label1" runat="server" Text='<%# Bind("Rating") %>'></asp:Label>
										<asp:Image ID="Image3" runat="server" />
									</ItemTemplate>
								</asp:TemplateField>
								<asp:BoundField DataField="Whois" HeaderText="Whois" SortExpression="Whois" />
							</Columns>
						</asp:GridView>
						<br />
						<br />
						<asp:Label ID="Label4" runat="server" Text="Visitor's Assessment"></asp:Label>
						<br />
						<asp:Label ID="Label5" runat="server" Text="Comment:"></asp:Label>
						<br />
						<asp:TextBox ID="TextBox2" runat="server" Height="86px" Width="385px"></asp:TextBox>
						<br />
						<br />
						<asp:Label ID="Label6" runat="server" Text="Rating"></asp:Label>
						<br />
						<asp:DropDownList ID="DropDownList1" runat="server">
							<asp:ListItem>1 - Extremely Nasty</asp:ListItem>
							<asp:ListItem>2 - Nasty</asp:ListItem>
							<asp:ListItem Selected="True">3 - Neutral</asp:ListItem>
							<asp:ListItem>4 - Safe</asp:ListItem>
							<asp:ListItem>5 - Very Safe</asp:ListItem>
						</asp:DropDownList>
						<br />
						<br />
						<asp:Button ID="btnComment" runat="server" Text="Submit" />
					</asp:Panel>
					<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
						ConnectionString="<%$ ConnectionStrings:tryConnectionString %>" 
						
						SelectCommand="SELECT tb_comments.Comment, tb_comments.Rating, tb_comments.Whois, tb_comments.Entry FROM tb_comments INNER JOIN tb_hijack ON tb_comments.Entry = tb_hijack.entry">
					</asp:SqlDataSource>
				</ItemTemplate>
			</asp:TemplateField>
			<asp:TemplateField HeaderText="Kind" SortExpression="kind">
				<EditItemTemplate>
					<asp:Image ID="Image2" runat="server" />
					<asp:Label ID="Label2" runat="server" Text='<%# Bind("kind") %>' 
						Visible="False"></asp:Label>
				</EditItemTemplate>
				<ItemTemplate>
					<asp:Image ID="Image2" runat="server" />
					<asp:Label ID="Label2" runat="server" Text='<%# Bind("kind") %>' 
						Visible="False"></asp:Label>
				</ItemTemplate>
				<ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
			</asp:TemplateField>
			<asp:TemplateField HeaderText="Rating" SortExpression="Expr1">
				<EditItemTemplate>
					<asp:Image ID="Image1" runat="server" Height="10px" Width="50px" />
					<br />
					<asp:Label ID="Label1" runat="server" Text='<%# Eval("Expr1") %>'></asp:Label>
				</EditItemTemplate>
				<ItemTemplate>
					<asp:Image ID="Image1" runat="server" Height="10px" Width="50px" />
					<br />
					<asp:Label ID="Label1" runat="server" Text='<%# Bind("Expr1") %>'></asp:Label>
				</ItemTemplate>
				<ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
			</asp:TemplateField>
			<asp:BoundField DataField="info" HeaderText="Info" SortExpression="info" >
			<ItemStyle VerticalAlign="Top" />
			</asp:BoundField>
			<asp:CommandField ShowEditButton="True">
			<ItemStyle VerticalAlign="Top" />
			</asp:CommandField>
		</Columns>
		<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
		<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
		<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
		<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
		<EditRowStyle BackColor="#7C6F57" />
		<AlternatingRowStyle BackColor="White" />
	</asp:GridView>
			<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
		ConnectionString="<%$ ConnectionStrings:tryConnectionString %>" 
		
		
		SelectCommand="SELECT tb_hijack.entry, tb_hijack.info, (SELECT rating FROM tb_comments WHERE (tb_hijack.entry = entry)) AS Expr1, tb_hijack.kind FROM tb_hijack INNER JOIN tb_comments AS tb_comments_1 ON tb_hijack.entry = tb_comments_1.entry">
	</asp:SqlDataSource>
			<br />
	<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
					
</asp:Content>



Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Finding control inside a template field using button field

Posted 17 December 2009 - 10:17 AM

Ah, you are using a ButtonField, not a Button. This is where you are going to set the CommandArgument property.

<asp:ButtonField ButtonType="Image" CommandName="show" CommandArgument="<%# Container.DataItemIndex %>"
				ImageUrl="~/pics/discuss.gif" Text="Button">
				<ItemStyle VerticalAlign="Top" />
			</asp:ButtonField>


Don't forget to add the code I previously mentioned to your code-behind in the RowCommand event.
Was This Post Helpful? 0
  • +
  • -

#5 jpcsmit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 26-September 09

Re: Finding control inside a template field using button field

Posted 17 December 2009 - 10:33 AM

I'm having a "Databinding expressions are only supported on objects that have a Databinding event" error...what does this mean?
Was This Post Helpful? 0
  • +
  • -

#6 jpcsmit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 26-September 09

Re: Finding control inside a template field using button field

Posted 22 December 2009 - 10:35 AM

I got it...though not entirely..hehe..I was able to get the control inside the template field..but how would I know what row the button field was clicked? Here's the code I got:

If e.CommandName = "show" Then
			For Each c As Control In GridView1.Rows(1).Controls
				For Each c1 As Control In c.Controls
					If c1.ID = "Panel1" Then
						If c1.Visible = True Then
							c1.Visible = False
						Else
							c1.Visible = True
						End If
					End If
				Next
			Next
End If



So here, I could make the panel show and hide but still need to indicate which row. How would I know what row was the button field clicked?
Was This Post Helpful? 0
  • +
  • -

#7 jpcsmit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 26-September 09

Re: Finding control inside a template field using button field

Posted 22 December 2009 - 10:48 AM

got it! i just used e.CommandArgument for the index..hehe..case closed...
Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Finding control inside a template field using button field

Posted 22 December 2009 - 02:01 PM

Excellent work, I knew you could do it. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1