assigning itemtemplate, edititemtemplate programmatically

Discussion in 'ASP .Net Datagrid Control' started by Steve P, Mar 28, 2005.

  1. Steve P

    Steve P Guest

    This really is a question for Datalist, since that is what I am primarily
    using; however it should apply to Datagrid as well.

    I would like to be able to assign the itemtemplate and edititemtemplate
    programmatically based on how the user would like the data layed out. I have
    been able to achieve this by using creating a class that implements the
    ITemplate interface, however I soon realized the drawback in that custom
    classes do not save their viewstate by default. Therefore I have to retrieve
    the data and assign the templates at every postback, which consumes resources
    and doesn't take advantage of the Viewstate properties of the
    datalist/datagrid. I tried adding my custom template class to the viewstate,
    but the error was that the object had to be serializable. I looked at this
    briefly but gave up due to time constraints.

    Having said all this, *ideally* what I would like is to be able to do is
    just have the html templates in separate files and then instantiate a class
    based on the user's preferred layout and assign it to the datalist. I think
    this is possible somehow but I don't have the time to research it right now.
    If anyone has done something like this before or would like to try, that
    would be great!!

    Steve P, Mar 28, 2005
    1. Advertisements

  2. Steve P

    Steve P Guest

    Okay, I found out that you can also use Page.LoadTemplate("<filename>.ascx")
    to assign the templates programmatically:

    DataList1.ItemTemplate = Page.LoadTemplate("itemtemplate1.ascx");

    This allows you to store the templates in different files, and assign them
    dynamically based on user input. However, there are still a few problems.
    First, you can't seem to be able to set DataList1.ExtractTemplateRows = true
    when doing this, because this causes the DataList to require that there be an
    asp table and doesn't recognize that my itemtemplate1.ascx actually contains
    a table. Second, and more importantly, postback is a major problem I've had
    with dynamically loading templates. This goes for doing it using a custom
    class that implements ITemplate and by using Page.LoadTemplate. It works
    fine if there are no command buttons. But if you do use, for instance, an
    Edit button (commandname=edit), then it will not work. The problem is that
    on the containing page (e.g. mydatalist.aspx), you want the page_load method
    to have the !IsPostBack requirement:

    if (!IsPostBack) {

    private void DataSub() {
    DataList1.DataSource = getDataSource();
    DataList1.ItemTemplate = Page.LoadTemplate("itemtemplate1.ascx");

    Unfortunately, if you have the !IsPostBack requirement, the page will not
    load DataSub() again initially when you click the edit button, but also the
    DataList1_EditCommand(...) method will never fire either!! The only way to
    get it to fire is to remove the !IsPostBack requirement; then, for some
    reason, the initial DataSub() will occur in page_load, and then the
    editcommand event will fire afterwards, followed by anothere DataSub(), which
    is totally inefficient:

    DataList1_EditCommand(object sender, System.EventArgs e) {
    DataList1.EditItemIndex = e.Item.ItemIndex;

    So, if you remove the DataSub() from the editcommand method, then DataSub
    only occurs once, but only BEFORE the edititemindex has been assigned.


    Does anyone have a proven method to dynamically assigning templates for the
    datagrid/datalist??? If so please let me know! :)

    Steve P, Mar 29, 2005
    1. Advertisements

  3. Steve P

    Steve P Guest

    Okay, found the solution to the problem about the edit button not firing.
    You have to assign the datalist templates in the page_init method as in the
    microsoft example at
    However, now I have another problem in that the editcommand actually fires
    twice instead of just once. If you copy the example code at the link above
    this is what you will find, unless there is something I'm doing wrong I will
    make a new post.
    Steve P, Mar 30, 2005
  4. Steve P

    Steve Guest

    Found the solution. It was related to how Visual studio generates code
    automatically to handle events. After I deleted this code ("#region Web Form
    Designer generated code") the button fired only once.
    Steve, Mar 30, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.