csetzkorn
asked on
creating a TemplateField in a GridView dynamically
Hi,
Usually, I am adding a TemplateField to a gridview like this:
<asp:TemplateField>
<itemTemplate>
<input class="btn" onclick="window.location = 'bla.aspx?bla_uid=<%# DataBinder.Eval(Container. DataItem, "bla_uid") %>'; return false;" name="more_details" id="more_details" type="submit" value="More Details" />
</itemTemplate>
</asp:TemplateField>
I would like to do the same dynamically from the code behind file.
I know how to add a boundfield:
bound_field = new BoundField();
bound_field.HeaderText = "Title";
bound_field.DataField = "title";
bound_field.SortExpression = "title";
grid_view.Columns.Add(boun d_field);
However, I havent found anything on TemplateFields. Any ideas?
Many thanks in advance.
BW,
Chris
Usually, I am adding a TemplateField to a gridview like this:
<asp:TemplateField>
<itemTemplate>
<input class="btn" onclick="window.location = 'bla.aspx?bla_uid=<%# DataBinder.Eval(Container.
</itemTemplate>
</asp:TemplateField>
I would like to do the same dynamically from the code behind file.
I know how to add a boundfield:
bound_field = new BoundField();
bound_field.HeaderText = "Title";
bound_field.DataField = "title";
bound_field.SortExpression
grid_view.Columns.Add(boun
However, I havent found anything on TemplateFields. Any ideas?
Many thanks in advance.
BW,
Chris
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
public class PageItemTemplate : System.Web.UI.ITemplate
{
System.Web.UI.WebControls.
private string columnName;
// ##########################
public PageItemTemplate(System.We
{
this.templateType = type;
this.columnName = columnName;
}
// ##########################
public void InstantiateIn(System.Web.U
{
Label l = new Label();
l.DataBinding += new EventHandler(this.l_DataBi
container.Controls.Add(l);
}
// ##########################
private void l_DataBinding(Object sender, EventArgs e)
{
// get the control that raised this event
Label l = (Label) sender;
// get the containing row
GridViewRow row = (GridViewRow) l.NamingContainer;
// get the raw data value and make it pretty
string RawValue = DataBinder.Eval(row.DataIt
Button b = new Button();
b.OnClientClick = @"window.location = 'page_details.aspx?page_ui
b.Text = "More Details";
b.CssClass = "btn";
l.Controls.Add(b);
}
// ##########################
}