Use Dynamic Loading templates and Ajax to implement refreshing operations

Source: Internet
Author: User
Use Dynamic Loading templates and Ajax to implement refreshing operations

Keywords: DataGrid dynamic template Ajax

When we use ajax to implement a refreshing interface, the list such as DataGrid and repeater is still troublesome. After my attempt, I found a reasonable method, you can avoid processing HTML, get the HTML content of the data list at any time, and Use templates in the List format to make it easier to maintain and read.

The principle is to write the template to the user control, use loadtemplate to load it, And then assign it to the DataGrid, repeater, and other controls. After binding, the HTML is output to the client.

. Aspx. CS
[Ajaxpro. ajaxmethod]
Public String getlisthtml (string old)
{
Repeater RPT = new repeater ();
Itemplate temp = page. loadtemplate ("webusercontrol1.ascx ");
Rpt. itemtemplate = temp;
Rpt. datasource = albums. getfolderlist (1 );
Rpt. databind ();

System. Text. stringbuilder sb = new system. Text. stringbuilder ();
System. Io. stringwriter Sw = new system. Io. stringwriter (SB );
Htmltextwriter writer = new htmltextwriter (SW );
Rpt. rendercontrol (writer );
Sw. Flush ();
Writer. Close ();
Sw. Close ();

Response. Write (sb. tostring ());
}

Webusercontrol1.ascx <% @ Control Language = "C #" %>
<% # Databinder. eval (repeateritem) container). dataitem, "Foldername") %> <br/>

Above is all of the ListCodeThe JS end can quickly obtain the templated list by calling class name. getlisthtml.

Corresponding to the DataGrid, you can add templatecollumn and other methods to template

Webusercontrol1.ascx
Webusercontrol1.ascx <% @ Control Language = "C #" %>
<% # Databinder. eval (datagriditem) container). dataitem, "Foldername") %> <br/>

Some codes of. aspx. CS:

DataGrid datagrid1 = new DataGrid ();
Itemplate temp = page. loadtemplate ("webusercontrol1.ascx ");
Templatecolumn Tc = new templatecolumn ();
TC. headertext = "test ";
TC. itemtemplate = temp;
Datagrid1.columns. Add (TC );
Datagrid1.autogeneratecolumns = false;
Datagrid1.datasource = albums. getfolderlist (1 );

Datagrid1.datamember = "ID ";
Datagrid1.databind ();

Full text

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.