LoadTemplate from a database?

Last post 03-20-2008 12:08 PM by FreshFrince. 2 replies.

Sort Posts:

  • LoadTemplate from a database?

    03-19-2008, 9:50 AM

    I have built a custom GridView that binds to user preferences to identify how the grid should be displayed.  The idea is that I define the available columns in the database and the user can choose to hide or display various columns.  In addition, they can specify the order of the columns and the default sort.  All of this is persisted in the database for subsequent sessions.

    As I am displaying the same type of data on various web forms, I would like to store the templates in the database with the other column setting info.  Then I can dynamically load the templates on all of my pages.  Which means I only define the template once.

    I found LoadTemplate which loads a template from a control file (ascx), but I would like to load a template from the database.  Is this possible?  Is there a better option?

    I also considered implementing ITemplate myself, but that seemed quite daunting.  I don't have a simple example of adding child controls to the template container.  Each template could be different and I want to employ the flexibility templates provide.  What I really need is the behavior of the ITemplate that the LoadTemplate method produces.  I don't want to recreate the wheel.
     


     

  • Re: LoadTemplate from a database?

    03-20-2008, 5:49 AM
    Answer

    Hi FreshFrince ,

    FreshFrince:
    I found LoadTemplate which loads a template from a control file (ascx), but I would like to load a template from the database.  Is this possible?  Is there a better option?

    From database is impossible.

    As far as I know , there are 2 ways to load template.

    One is to use LoadTemplate method which loads a template from a control file (ascx), and you are using this way now.

    Another way is to implement ITemplate yourself and set it to gridview in codebehind.

        public class CreateItemTemplateLabel : ITemplate
        {
            string strColumnText;
            string strLabelName;
            public CreateItemTemplateLabel(string ColText, string LabelName)
            {
                this.strColumnText = ColText;
                this.strLabelName = LabelName;
            }
            public void InstantiateIn(Control objContainer)
            {
                Label lbl = new Label();
                lbl.ID = strLabelName;
                lbl.DataBinding += new EventHandler(lbl_DataBinding);
                objContainer.Controls.Add(lbl);
            }
            private void lbl_DataBinding(object sender, EventArgs e)
            {
                Label lbl = (Label)sender;
                lbl.ID = strLabelName;
                lbl.Text = strColumnText;
            }
        }
     

     

     

     

    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: LoadTemplate from a database?

    03-20-2008, 12:08 PM

    Thanks for the response Samu.  What you have described is what I am doing now (except I am using a literal).  Unfortunately, what I was trying to accomplish is more dynamic templates.  In my custom gridview, most columns are displayed as literal text.  However, I have the need in a couple of places to render the literal text plus a button.  I expect that I will have other custom template needs in the future.

    As the example you gave covers 95% of my use cases, I will continue with that and consider overriding RowDataBound to handle the others.  Another option is to create a custom ITemplate implementation for each customization that I need.

    Thanks.

     

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter