Last post Oct 26, 2012 02:51 PM by meganc99
Jun 21, 2006 09:40 PM|ckormann|LINK
From code I need to dynamically create a column for a GridView. This column must be databound and it must have a hyperlink. If I were doing this from the ASPX source it is simple enough to create a TemplateColumn, but I am having difficulty doing it from
So, irrespective of the type of column that is created, from code I need to dynamically create a column that is databound and has a hyperlink. How can I do this?
Thanks for your help.
Jun 22, 2006 12:57 PM|kushi|LINK
I have given code for dynamically adding template column with hyperlink to GridView.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT [au_id], [au_lname], [au_fname] FROM [authors]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="au_id"
private void BuildTemplateColumnsDynamically()
BoundField fname = new BoundField();
fname.DataField = "au_fname";
fname.HeaderText = "Author FirstName";
TemplateField lname = new TemplateField();
lname.HeaderText = "Author LastName";
lname.ItemTemplate = new AddTemplateToGridView(ListItemType.Item, "au_lname");
public class AddTemplateToGridView : ITemplate
public AddTemplateToGridView(ListItemType type, string colname)
_type = type;
_colName = colname;
void ITemplate.InstantiateIn(System.Web.UI.Control container)
HyperLink ht = new HyperLink();
ht.Target = "_blank";
void ht_DataBinding(object sender, EventArgs e)
HyperLink lnk = (HyperLink)sender;
GridViewRow container = (GridViewRow)lnk.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, _colName);
if (dataValue != DBNull.Value)
lnk.Text = dataValue.ToString();
lnk.NavigateUrl = "http://www.booktitles.com";
Jun 22, 2006 04:52 PM|ckormann|LINK
Excellent! I worked perfectly. Thanks so much for your help.
Jun 22, 2006 08:50 PM|CLprogrammer|LINK
I just tried the same thing except using textboxes inside my item templates and it works, but when I post back the columns are added again except without textboxes in the item templates. If I put the code in a if(!ISPostback) then the textboxes get removed
from the columns on post back. Any ideas what problem could be? Heres my code as I used it.
lname.HeaderText = code;
lname.ItemStyle.Width = 30;
_type = type;
_colName = colname;
tbx.Width = 30;
tbx.Text = dataValue.ToString();
Jul 13, 2006 01:44 PM|Caffeine|LINK
Either call DataBind on the gridview on the postback or recreate the gridview in Page_Init.
The root problem is that InstantiateIn() is called when the gridview databinds.
Sep 07, 2011 07:10 AM|suryakant4it|LINK
what is Codes? that is used in foreach loop
Oct 26, 2012 02:51 PM|meganc99|LINK
I've been searching online for 2 days about this and from what I understand dynamically created TemplateFields will be lost on post back. If this is the case how are we supposed to get the values entered into the templated textboxes on Save of a button?
This is what I need to do:
I have managed to do upto step 3. I'm stuck at step 4.
On click of Save button, how do I get the values user entered into textboxes? I check to see if there are any rows in the gridview and I get 0 rows, assuming the gridview is getting lost on the post back.
Is there a way around this? Am on the right track or missing something?
Please, please help! Any help will be greatly appreciated.
Thank you very much.