12 Replies - 57282 Views - Last Post: 21 January 2010 - 12:48 PM Rate Topic: -----

#1 jpcsmit  Icon User is offline

  • New D.I.C Head

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

How to get edit template controls in gridview

Posted 18 January 2010 - 07:50 PM

I have a gridview with template field on column 2 and 4. I need to get the control (dropdownlist and textbox) on these columns so that I could get their values to update SQL DB. I'm programmatically querying the db and i'm not using the smart tag of the gridview. Is there any way to get controls of the edit template? Thanks!!

Here's my source code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
		CellPadding="2" DataKeyNames="entry" 
		ForeColor="Black" GridLines="None" BackColor="LightGoldenrodYellow" 
			BorderColor="Tan" BorderWidth="1px">
		<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="lblComment" 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="lblComment" runat="server" Text='<%# Bind("entry") %>'></asp:Label>
					<br />
					<br />
					<asp:Panel ID="Panel1" runat="server" style="margin-right: 0px" Visible="False">
						<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
							DataSourceID="SqlDataSource2">
							<Columns>
								<asp:TemplateField HeaderText="Rating" SortExpression="Rating">
									<EditItemTemplate>
										<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Rating") %>'></asp:TextBox>
									</EditItemTemplate>
									<ItemTemplate>
										<asp:Label ID="Label9" runat="server" Text='<%# Bind("Rating") %>' 
											Visible="False"></asp:Label>
										<asp:Image ID="Image3" runat="server" />
									</ItemTemplate>
								</asp:TemplateField>
								<asp:BoundField DataField="Comment" HeaderText="Comment" 
									SortExpression="Comment" />
								<asp:BoundField DataField="Whois" HeaderText="User" SortExpression="Whois" />
							</Columns>
						</asp:GridView>
						<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
							ConnectionString="<%$ ConnectionStrings:hijackConnectionString %>" 
							SelectCommand="SELECT tb_entity.Entry, tb_comments.Comment, tb_comments.Rating, tb_comments.Whois FROM tb_entity INNER JOIN tb_comments ON tb_entity.id = tb_comments.Entry WHERE (tb_entity.Entry = @Entry)">
							<SelectParameters>
								<asp:ControlParameter ControlID="lblComment" Name="Entry" PropertyName="Text" />
							</SelectParameters>
						</asp:SqlDataSource>
						<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" 
							onclick="btnComment_Click" />
					</asp:Panel>
				</ItemTemplate>
				<ItemStyle Font-Size="Small" />
			</asp:TemplateField>
			<asp:TemplateField HeaderText="Kind" SortExpression="kind">
				<EditItemTemplate>
					<asp:Image ID="imgKind" runat="server" Height="16px" Visible="False" />
					<asp:Label ID="lblKind" runat="server" Text="Label" Visible="False"></asp:Label>
					<br />
					<asp:DropDownList ID="DropDownList1" runat="server" 
						onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
						style="margin-bottom: 0px; margin-right: 0px;">
						<asp:ListItem Selected="True" Value="2">Nasty</asp:ListItem>
						<asp:ListItem Value="3">Unknown</asp:ListItem>
						<asp:ListItem Value="1">Good</asp:ListItem>
						<asp:ListItem Value="4">Windows</asp:ListItem>
						<asp:ListItem Value="5">AV Scanner</asp:ListItem>
						<asp:ListItem Value="6">Firewall</asp:ListItem>
						<asp:ListItem Value="7">Unneeded</asp:ListItem>
					</asp:DropDownList>
				</EditItemTemplate>
				<ItemTemplate>
					<asp:Image ID="imgKind" runat="server" Height="30px" Width="27px" />
					<asp:Label ID="lblKind" 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="imgRating" runat="server" Height="10px" Width="50px" />
					<br />
					<asp:Label ID="lblRating" runat="server" Text='<%# Eval("rating") %>'></asp:Label>
				</EditItemTemplate>
				<ItemTemplate>
					<asp:Image ID="imgRating" runat="server" Height="11px" Width="40px" />
					<br />
					<asp:Label ID="lblRating" runat="server" Text='<%# Bind("rating") %>' 
						Visible="False"></asp:Label>
				</ItemTemplate>
				<ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
			</asp:TemplateField>
			<asp:TemplateField HeaderText="Info" SortExpression="info">
				<EditItemTemplate>
					<asp:TextBox ID="TextBox3" runat="server" Height="44px" 
						Text='<%# Bind("info") %>' TextMode="MultiLine" Width="300px"></asp:TextBox>
				</EditItemTemplate>
				<ItemTemplate>
					<asp:Label ID="Label7" runat="server" Text='<%# Bind("info") %>'></asp:Label>
				</ItemTemplate>
				<ItemStyle VerticalAlign="Top" Font-Size="Small" />
			</asp:TemplateField>
			<asp:CommandField ShowEditButton="True">
			<ItemStyle VerticalAlign="Top" />
			</asp:CommandField>
		</Columns>
		<FooterStyle BackColor="Tan" />
		<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" 
			HorizontalAlign="Center" />
		<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
		<HeaderStyle BackColor="Tan" Font-Bold="True" />
		<AlternatingRowStyle BackColor="PaleGoldenrod" />
	</asp:GridView>



Is This A Good Question/Topic? 0
  • +

Replies To: How to get edit template controls in gridview

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to get edit template controls in gridview

Posted 18 January 2010 - 09:27 PM

Youj can use the FindControl method of a row to get a control in one of the columns. If you don't have a solution by tomorrow morning, I will get you some code.
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: How to get edit template controls in gridview

Posted 20 January 2010 - 12:13 PM

thanks eclipse. no i still don't have an idea on this one. i've already used find control and it's not finding it. i've also used msgbox to output all column four's control ID and it's returning nothing. I think you're referring to Item template here. Yes, i could see the controls in item template, but not in edititemtemplate. that's why i don't know how to go about it.

the reason behind why i can't use the smart tag is because upon loading of the page, it would accept strings or text file to parse then from there, it would query the sql db. when i use smart tag, upon loadng of the page, it queries all data in the db and show it to the page i don't want that to happen. i mean i could hide the gridview upon loading but still, it would query the sql db and i don't want the server to have that burden.

if you have an alternative solution to this one then i'm all ears. it would be alot easier for me if i'll be using smart tag. :) thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to get edit template controls in gridview

Posted 20 January 2010 - 12:42 PM

An EditItemTemplate in a GridView? I didn't know there was such a thing.
Attached Image

I don't see it as an option.
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

Re: How to get edit template controls in gridview

Posted 20 January 2010 - 01:44 PM

@eclipsed4utoo: Then you have not truly delved into GridViews, grasshopper. :)

In order to get to the EditItemTemplate, you need to first create a TemplateField column.
Using TemplateFields in the GridView Control

	<asp:GridView ID="GridView1" runat="server">
		<Columns>
			<asp:TemplateField>
				<ItemTemplate></ItemTemplate>
				<EditItemTemplate></EditItemTemplate>
			</asp:TemplateField>
		</Columns>
	</asp:GridView>


eclipsed4utoo was on the right track, as far as how to get to the controls. You will use the FindControl method. But since the controls are buried in a TemplateField, you cannot just do a find control on the Row. It will not find it, you need to be more specific than that.

A very important note, in order to get the control in the EditItemTemplate, then the GridView must be in Edit mode. Otherwise you will just get the control in the ItemTemplate.

I am a little confused as to why you are programmatically querying and updating data when you are obviously using an SqlDataSource.

Why not just let the SqlDataSource handle the update?
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: How to get edit template controls in gridview

Posted 20 January 2010 - 02:43 PM

i couldn't use sqldatasource because i need to make a for loop for the textbox/text file user input to get all data to query. Here's the code for that.
If Not strline Is Nothing Then
						   If SQL = "" Then
								SQL = "SELECT tb_entity.Entry, (SELECT avg(rating) FROM tb_comments WHERE " _
								& "tb_comments.entry=tb_entity.id) as Rating, tb_kind.Kind, tb_hijack.Info " _
								& "FROM tb_entity INNER JOIN tb_hijack ON tb_entity.id = tb_hijack.Entry " _
								& "INNER JOIN tb_kind ON tb_hijack.Kind = tb_kind.id " _
								& "WHERE tb_entity.entry='" & strline.Replace("'", "''") & "'"
							Else
								SQL = SQL & " OR tb_entity.entry='" & strline.Replace("'", "''") & "'"
							End If
End If



Also if i use that, then upon loading of the page, it executes the SELECT command w/c gets all the data from the sql db. The page works this way:
1. user either input strings or upload a text file with strings to parse.
2. page would read the strings from either text box or text file then do a SELECT FROM from the sql db

what i would like is upon loading, it wouldn't query the sql db. the datagrid wouldn't show up since it doesn't have any data in it. it would only do a query once user enters strings to textbox or enters text file to parse. would there be a way for that?

Also, i already did the msgbox() to output controls in gridview in row updating event to make sure the gridview is in edititemtemplate (or so i think it is) and didn't show any control id. Also, the fourth column is already a template field.

This post has been edited by jpcsmit: 20 January 2010 - 02:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

Re: How to get edit template controls in gridview

Posted 20 January 2010 - 06:04 PM

Ok, so post the code you are using in the RowUpdating event.
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to get edit template controls in gridview

Posted 21 January 2010 - 04:31 AM

View PostJayman, on 20 Jan, 2010 - 04:44 PM, said:

@eclipsed4utoo: Then you have not truly delved into GridViews, grasshopper. :)

In order to get to the EditItemTemplate, you need to first create a TemplateField column.
Using TemplateFields in the GridView Control


Ahh yes, I have used those since that's the only way to make a checkbox that a user can check in a gridview.
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

Re: How to get edit template controls in gridview

Posted 21 January 2010 - 08:29 AM

For future reference, there is a CheckBoxField that is built into the GridView.
Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to get edit template controls in gridview

Posted 21 January 2010 - 09:39 AM

View PostJayman, on 21 Jan, 2010 - 11:29 AM, said:

For future reference, there is a CheckBoxField that is built into the GridView.


Yes there is, but that's a BoundField, which means the user can't change the value in the GridView. For the user to be able to check the checkbox, it needs to be a TemplateField.

For example:

<div>
	<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
		<Columns>
			<asp:CheckBoxField Text="Check Here" DataField="checked" />
		</Columns>
	</asp:GridView>
</div>



protected void Page_Load(object sender, EventArgs e)
{
	if (!Page.IsPostBack)
	{
		DataTable dt = new DataTable();
		dt.Columns.Add("checked", typeof(bool));

		DataRow row = dt.NewRow();
		row["checked"] = true;
		dt.Rows.Add(row);

		row = dt.NewRow();
		row["checked"] = false;
		dt.Rows.Add(row);

		GridView1.DataSource = dt;
		GridView1.DataBind();
	}
}



You will see that the checkboxes are greyed out.
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to get edit template controls in gridview

Posted 21 January 2010 - 09:48 AM

now if I turn it into TemplateField, I can now click the checkbox, and determine if it was checked.

<div>
	<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
		<Columns>
			<asp:CheckBoxField Text="Check Here" DataField="checked" />
			<asp:TemplateField>
				<EditItemTemplate>
					<asp:CheckBox ID="chkHere" runat="server" Text="Here 1" />
				</EditItemTemplate>
				<ItemTemplate>
					<asp:CheckBox ID="chkHere" runat="server" Text="Here 2" />
				</ItemTemplate>
			</asp:TemplateField>
		</Columns>
	</asp:GridView>
	<asp:Button ID="button1" runat="server" onclick="button1_Click" Text="Now" />
	<asp:Label ID="lblInformation" runat="server" />
</div>



protected void button1_Click(object sender, EventArgs e)
{
	for (int i = 0; i < GridView1.Rows.Count; i++)
	{
		CheckBox cb = GridView1.Rows[i].FindControl("chkHere") as CheckBox;

		if (cb.Checked)
		{
			lblInformation.Text = cb.Text + " checked";
		}
	}
}



Now this does get the control that is in the ItemTemplate, not the EditItemTemplate. However, atleast for the checkbox, it does contain the modified value. If you run the code above, and check one of the checkboxes, you will see that the code recognizes that the checkbox is now checked.
Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: How to get edit template controls in gridview

Posted 21 January 2010 - 09:59 AM

So to the OP, here is some code that I just tested to work.

here is a simple grid with one field that is a DropDownList. It's prepopulated with two list items just for simplicity.
<div>
	<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
		<Columns>
			<asp:TemplateField>
				<EditItemTemplate>
					<asp:DropDownList ID="ddlTest" runat="server">
						<asp:ListItem Text="hey1" />
						<asp:ListItem Text="hey2" />
					</asp:DropDownList>
				</EditItemTemplate>
				<ItemTemplate>
					<asp:DropDownList ID="ddlTest" runat="server">
						<asp:ListItem Text="hey1" />
						<asp:ListItem Text="hey2" />
					</asp:DropDownList>
				</ItemTemplate>
			</asp:TemplateField>
		</Columns>
	</asp:GridView>
	<asp:Button ID="button1" runat="server" onclick="button1_Click" Text="Now" />
	<asp:Label ID="lblInformation" runat="server" />
</div>



when the button is clicked, you get the control, then output the text from the dropdown..
protected void Page_Load(object sender, EventArgs e)
{
	if (!Page.IsPostBack)
	{
		DataTable dt = new DataTable();
		dt.Columns.Add("checked", typeof(bool));

		DataRow row = dt.NewRow();
		row["checked"] = true;
		dt.Rows.Add(row);

		GridView1.DataSource = dt;
		GridView1.DataBind();
	}
}

protected void button1_Click(object sender, EventArgs e)
{
	for (int i = 0; i < GridView1.Rows.Count; i++)
	{
		DropDownList ddl = GridView1.Rows[i].FindControl("ddlTest") as DropDownList;

		lblInformation.Text = ddl.Text;
	}
}



see if that helps you out.
Was This Post Helpful? 0
  • +
  • -

#13 Jayman  Icon User is offline

  • Student of Life
  • member icon

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

Re: How to get edit template controls in gridview

Posted 21 January 2010 - 12:48 PM

View Posteclipsed4utoo, on 21 Jan, 2010 - 09:39 AM, said:

Yes there is, but that's a BoundField, which means the user can't change the value in the GridView. For the user to be able to check the checkbox, it needs to be a TemplateField.


True, I read right past where you said you wanted the user to be able to check/uncheck the CheckBox. My bad.. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1