In ASP. NET, the templates (Template) function is often used, for example, in the DataGrid, datalist, repeater and other controls, the use of templates will greatly enhance its function. In the past, we generally designed Program You have already set the template in the control. However, sometimes, we may need to dynamically load templates. For example, when you want your application's interface style to change with your needs, you need the function of dynamically loading templates. However, it should be noted that not all Web controls support the template function. Also, note which controls support the template function. The following lists some controls that support the template function:
The Repeater control supports the following templates:
Headertemplate, footertemplate, itemtemplate, alternatingitemtemplate, seperatortemplate.
Datelist control. Supported Templates include:
Headertemplate, footertemplate, itemtemplate, alternatingitemtemplate, separatortemplate, selecteditemtemplate, edititemtemplate.
DataGrid Control. Supported Templates include:
Headertemplate, footertemplate, itemtemplate, edititemtemplate, pager.
Next, I will use a template to dynamically load the datalist control to illustrate how to dynamically load the template:
First, let's understand the principle of dynamic loading templates. In. net, there is a templatecontrol class, which is the base class of the page and usercontrol classes. It also defines the basic functions of the page and usercontrol classes. This class provides two methods: loadcontrol and loadtemplate. The loadcontrol method loads controls from external files and returns the usercontrol class object. The loadtemplate method loads the template from an external file and returns the itemplate object.
In the loadtemplate method, there is only one parameter. The parameter value is the path of the external template file and the itemplate object is returned. The datalist control provides a series of attributes that can be used to set attributes of various templates, including alternatingitemtemplate, edititemtemplate, footertemplate, headertemplate, itemtemplate, selecteditemtemplate, and seperatortemplate, you will see the introduction.
Next, we will introduce the example. In the example program, data tables and columns are dynamically created and encapsulated into a DB class, this allows readers to further review how to dynamically create data tables and columns without extracting data from the database (of course, you can also use the traditional method of reading the database ),
Public class DB { Public dB () {} /// <Summary> /// Method returns a DataSet object filled with data /// </Summary> Public static dataset getdataset () { // Create dataset and datatable Dataset DS = new dataset (); Datatable table = new datatable ("records "); Datacolumn Col; // Add a column Col = new datacolumn (); Col. 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 [& quot; Id & quot;] = 1001; Row ["name"] = "meleni giard "; Row ["Address"] = "23rd Street, Park Road, NY City, NY "; Table. Rows. Add (ROW ); Row = table. newrow (); Row [& quot; Id & quot;] = 1002; Row ["name"] = "Puneet nehra "; Row ["Address"] = "3rd Blvd, Ashok Vihar, New Delhi "; Table. Rows. Add (ROW ); Row = table. newrow (); Row [& quot; Id & quot;] = 1003; Row ["name"] = "AJ Mehta "; Row ["Address"] = "nagrath Chowk, jabalpur "; Table. Rows. Add (ROW ); Row = table. newrow (); Row [& quot; Id & quot;] = 1004; Row ["name"] = "Max Muller "; Row ["Address"] = "25 North Street, hernigton, Russia "; Table. Rows. Add (ROW ); // Add datatable to Dataset DS. Tables. Add (table ); // Return Dataset Return Ds; } } |
Next, we will first create several template files. Create two sets of template files, each containing four template files: Header, footer, item, and alternating item, and save them. in this way, we have two types of templates. Each type of template has its own header, footer, item, alternating item subtemplate. The following is an item template file, which is similar to others.
<% @ Control Language = "VB" %> <Font face = "verdana" color = "green" size = "2"> <B> ID: </B> <% # Databinder. eval (ctype (container, datalistitem). dataitem, "ID") %> <B> name: </B> <% # Databinder. eval (ctype (container, datalistitem). dataitem, "name") %> <Br> <B> address: </B> <% # Databinder. eval (ctype (container, datalistitem). dataitem, "Address") %> <P> </Font> |
Finally, create an application, create a project, and add two buttons and a datalist control, as shown in figure
Then create a binddatagrid method and bind the dataset to the datalist control,CodeAs follows:
Private void binddatagrid () { Dtset = dB. getdataset (); Datalist1.datasource = dtset. Tables [0]. defaultview; Datalist1.databind (); } Private void page_load (Object sender, system. eventargs E) { If (! Ispostback) { Binddatagrid (); } } |
Finally, add code for the clcik events of the two buttons, and use the page. loadtemplate method to load the templates in the two template groups we have prepared. The Code is as follows.
Private void button#click (Object sender, system. eventargs E) { // Load templates Datalist1.alternatingitemtemplate = Page. loadtemplate ("altitemtempate. ascx "); Datalist1.itemtemplate = page. loadtemplate ("itemtemplate. ascx "); Datalist1.headertemplate = page. loadtemplate ("headtemplate. ascx "); Datalist1.footertemplate = page. loadtemplate ("foottemplate. ascx "); Binddatagrid (); } Private void button2_click (Object sender, system. eventargs E) { // Load templates Datalist1.alternatingitemtemplate = page. loadtemplate ("altitemtempate2.ascx "); Datalist1.itemtemplate = page. loadtemplate ("itemtemplate2.ascx "); Datalist1.headertemplate = page. loadtemplate ("headtemplate2.ascx "); Datalist1.footertemplate = page. loadtemplate ("foottemplate2.ascx "); Binddatagrid (); } |
The running effect is shown in the following two figures. Different template styles are dynamically loaded when different buttons are clicked.