asp.net| Dynamic | load 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:
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),
public class DB
{
Public DB ()
{ }
<summary>
method returns a DataSet object filled with data
</summary>
public static DataSet GetDataSet ()
{
Creating Datasets 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);
Add DataTable to DataSet
Ds. Tables.add (table);
Return DataSet
return 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.
<%@ 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, we started creating the application, building a new project, adding two buttons and a DataList control as shown below
Then create a Binddatagrid method, bind the DataSet to the DataList control, and the code is as 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 the 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.
private void Button1_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 effect is as follows two figure, when the different button, dynamic load different template style.
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.