Dynamic loading of template in ASP.NET

zhaozj2021-02-12  157

ASP.NET often uses the Templates feature, such as in the controls such as DataGrid, DataList, Repeater, which will greatly enhance its functionality. In the past, we generally set the template in the control when designing the program. However, sometimes, we may need a dynamic loading template. For example, when you ask your application's interface style as the user's needs, you need to dynamically load the template. But not to pay attention, not all web controls support template functions, but also pay attention to what functions of which controls support the template, and some controls for supporting template feature are listed below:

Repeater control, supported template:

HeadeRtemplate, Footertemplate, ItemTemplate, AlternatingItemTemplate, SEPERATORTEMPLATE.

DateList control, supported template:

HeadeRTemplate, Footertemplate, ItemTemplate, AlternatingItemTemplate, SeparatorTemplate, SELECTEDITEMPLATE, EDITITEMTEMPLATE.

DataGrid controls, supported templates:

Headertemplate, Footertemplate, ItemTemplate, EditItemplate, Pager.

Below, I will explain how to dynamically load the template to dynamically load the DataList control:

First understand the principle of dynamic loading template. In .NET, there are TemplateControl classes, this class 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 ways: loadingControl and LoadTemplate. The LoadControl method is loaded from external files and returns a 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 returns the Itemplate object. The DatAlist control provides a range of properties that set properties of various templates, including AlternatingItemTemplate, EditItemTemplate, FooterTemplate, HeadeRPlate, and SEPERTORTEMPLATE, which will be described hereinafter.

Next, we have begun to introduce examples. In the sample program, we use dynamically created data sheets and data columns, and create the creation of the data into a DB class, so that the reader further reviews how to dynamically create data sheets, data columns, etc. It is not used to extract from the database (of course, you can also use the traditional way of reading the database),

Public class db {public db () {} ///

/// method returns a Dataset Object Filled with data /// 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 (color); 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 (color); col = new datacolumn () Col.datatype = system.type.gettype ("system.string"); columnnname = "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 [""] = "Melanie Giard"; Row ["Address"] = "23RD Street, Park Road, NY City, NY "; Table.Rows.Add (Row); 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"] = "25 North Street, Hernigton, Russia"; Table.Rows.Add (Row); // Add DataTable to DataSet DS.Tables.Add (Table); // Return DataSet Return DS;} }

Next, we first create a number of template files. Let's create two sets of template files, each set of template files contain four template files, saved into .ascx files, which, we have two types of style templates, each type of style The template has its own header, footer, item, alternating itemon template. Here is one of the Item template files, others like.

<% @ Control Language = "VB"%> id: <% # databinder.eval (CType (Container, DatalistItem ) .DataItem, "ID")%> Name: <% # databinder.eval (ctype (container, datalistitem) .dataItem, "name")%>
address: < / b> <% # DataBinder.eval (CType (CTYPE (CONTAINER, DATALISTITEM) .DataItem, "Address")%>

Finally, we started to create an app, create a new project, add two buttons and a Datalist control as follows

转载请注明原文地址:https://www.9cbs.com/read-6325.html

New Post(0)