[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

creating a TemplateField in a GridView dynamically

Posted on 2007-08-06
2
Medium Priority
?
6,149 Views
Last Modified: 2012-08-13
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(bound_field);

However, I havent found anything on TemplateFields. Any ideas?

Many thanks in advance.

BW,

Chris
0
Comment
Question by:csetzkorn
2 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 2000 total points
ID: 19639138
Dynamic Template Columns in the ASP.NET 2.0 GridView Control
http://www.developer.com/net/asp/article.php/10917_3609991_1
0
 

Author Comment

by:csetzkorn
ID: 19639520
Thanks, I was actually toying with a ITemplate implementation but did not get to work. However, the example you have provided me with give me enough info to get it to work:

public class PageItemTemplate : System.Web.UI.ITemplate
{
    System.Web.UI.WebControls.ListItemType templateType;
    private string columnName;

    // ####################################################################

    public PageItemTemplate(System.Web.UI.WebControls.ListItemType type, string columnName)
      {
            this.templateType = type;
        this.columnName = columnName;
      }

    // ####################################################################

    public void InstantiateIn(System.Web.UI.Control container)
    {
        Label l = new Label();
        l.DataBinding += new EventHandler(this.l_DataBinding);
        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.DataItem, columnName).ToString();

        Button b = new Button();
        b.OnClientClick = @"window.location = 'page_details.aspx?page_uid=" + RawValue + "'; return false;";
        b.Text = "More Details";
        b.CssClass = "btn";
        l.Controls.Add(b);

    }

    // ####################################################################
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question