Asp. Dynamic loading of template in net

Source: Internet
Author: User
Tags eval header net string

Asp. NET, often use the templates (template) features, such as in Datagrid,datalist,repeater and other controls, the use of templates, will greatly enhance its functionality. In the past, when we were designing a program, we had already set the template in the control. However, sometimes, we may need to dynamically load templates, for example, when you ask your application's interface style to change with the user's needs, you need to go to the dynamic Loading template function. Note, however, that not all Web controls support the template feature, and note which controls support the functionality of the template, and some of the controls that support the template feature are listed below:

Repeater control, supported templates are:

HeaderTemplate, FooterTemplate, ItemTemplate, AlternatingItemTemplate, Seperatortemplate.

Datelist control, supported templates are:

HeaderTemplate, FooterTemplate, ItemTemplate, AlternatingItemTemplate, SeparatorTemplate, SelectedItemTemplate, EditItemTemplate.

The DataGrid control, supported templates are:

HeaderTemplate, FooterTemplate, ItemTemplate, EditItemTemplate, Pager.

Below, I'll show you how to dynamically load a template by dynamically loading the template for the DataList control:

The first step is to understand the principle of dynamic loading templates. In. NET, there is the TemplateControl class, which is the base class for page and UserControl classes. It also defines the basic functions of page and UserControl classes. This class provides two methods: LoadControl and LoadTemplate. The LoadControl method loads the control from the external file and returns the UserControl class object. The LoadTemplate method loads the template from the external file and returns the ITemplate object.

In the LoadTemplate method, there is only one parameter, the parameter value is the path to the external template file, and the ITemplate object is returned. The DataList control provides a series of properties that allow you to set properties for various templates, including AlternatingItemTemplate, EditItemTemplate, FooterTemplate, HeaderTemplate, ItemTemplate, SelectedItemTemplate, and Seperatortemplate, in the following section, will see the relevant introduction.

Next, let's start with an example, in the sample program, is to use dynamic creation of data tables and data columns, and to encapsulate the creation of the data into a DB class, so that readers can further review how to dynamically create data tables, columns, etc., and not be extracted from the database (of course, you may also use the traditional method of reading the database),

The following are the referenced contents:
public class Db{public DB () {}///<summary>///method returns a DataSet object filled with data///</summary> public static DataSet GetDataSet () {//Create DataSet and Datatabledataset ds = new DataSet ();D atatable table = new DataTable ("Records");D Atacolumn col;//Add a column col = new DataColumn (); DataType = System.Type.GetType ("System.Int32"); Col. ColumnName = "ID"; Col. ReadOnly = True;col. Unique = true;table. Columns.Add (col); col = new DataColumn (); Col. DataType = System.Type.GetType ("System.String"); Col. ColumnName = "Name"; Col. AutoIncrement = False;col. Caption = "Name"; Col. ReadOnly = False;col. Unique = false;table. Columns.Add (col); col = new DataColumn (); Col. DataType = System.Type.GetType ("System.String"); Col. ColumnName = "Address"; Col. AutoIncrement = False;col. Caption = "Address"; Col. ReadOnly = False;col. Unique = false;table. Columns.Add (col);//Add a record datarow row = table. NewRow (); row["ID"] = 1001;row["Name"] = "Melanie giard"; row["Address" = "23rd Street, Park Road, NY, NY"; table. Rows.Add (row); = table. NewRow (); row["ID"] = 1002;row["Name"] = "Puneet Nehra"; row["Address" = "3rd Blvd, Ashok Vihar, New Delhi"; table. Rows.Add (row), row = table. NewRow (); row["ID"] = 1003;row["Name" = "Raj Mehta"; row["Address" = "Nagrath Chowk, Jabalpur"; table. Rows.Add (row), row = table. NewRow (); row["ID"] = 1004;row["Name"] = "Max Muller"; row["Address" = "n ' street, Hernigton, Russia"; table. Rows.Add (row);//Add DataTable to DataSetds.Tables.Add (table);//Return Datasetreturn DS;}}

Next, we first create several template files. We first create two sets of template files, each containing a header,footer,item,alternating item four template file, Saved as an. ascx file, we have two types of style templates, each type of style template has its own header,footer,item,alternating item template. The following is one of the item template files, others are similar.

The following are the referenced contents:
<%@ control language= "VB"%> ID: <%# DataBinder.Eval (CType , DataListItem). DataItem, "ID")%> Name: <%# DataBinder.Eval (CType (Container, DataListItem). DataItem, "Name")%>
Address: <%# DataBinder.Eval (CType (Container, DataListItem). DataItem, "Address")%>

Finally, we started creating the application, building a new project, adding two buttons and a DataList control to the following figure:

Then create a Binddatagrid method, bind the DataSet to the DataList control, and the code is as follows:

The following are the referenced contents:
private void Binddatagrid () {dtset = DB. GetDataSet ();D Atalist1.datasource = Dtset.tables[0]. Defaultview;datalist1.databind ();} private void Page_Load (object sender, System.EventArgs e) {if (! IsPostBack) {Binddatagrid ();}}

Finally, add code for the two-button Clcik event, respectively, using the Page.loadtemplate method to load the templates in the two sets of templates that we have already written, the code is as follows:

The following are the referenced contents:
private void Button1_Click (object sender, System.EventArgs e) {//Load templatesdatalist1.alternatingitemtemplate = Page.loadtemplate ("Altitemtempate.ascx");D atalist1.itemtemplate =page.loadtemplate ("ItemTemplate.ascx");D Atalist1.headertemplate =page.loadtemplate ("Headtemplate.ascx");D atalist1.footertemplate = Page.LoadTemplate (" Foottemplate.ascx "); Binddatagrid ();} private void Button2_Click (object sender, System.EventArgs e) {//Load templatesdatalist1.alternatingitemtemplate = Page.loadtemplate ("Altitemtempate2.ascx");D atalist1.itemtemplate = page.loadtemplate ("ItemTemplate2.ascx");D Atalist1.headertemplate = Page.loadtemplate ("Headtemplate2.ascx");D atalist1.footertemplate = Page.LoadTemplate (" Foottemplate2.ascx "); Binddatagrid ();}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.