Dynamic template columns in gridview

Source: Internet
Author: User

Recently, the columns that use the gridview function are dynamically generated, and the template column is used. The method for searching the Internet is as follows:

1. Write a class to inherit itemplate

 

Code
Public   Class Gridviewtemplate: itemplate
{
Private Datacontrolrowtype templatetype; // Differentiate column types
Private   String Columnname; // Name

Public Gridviewtemplate (datacontrolrowtype type, String Colname) // Structure
{
Templatetype = Type;
Columnname = Colname;
}

Public   Void Instantiatein (system. Web. UI. Control container)
{
Switch (Templatetype)
{
Case Datacontrolrowtype. header: // Column header
Literal LC =   New Literal ();
LC. Text =   " <B> "   + Columnname +   " </B> " ; // Column header name
Container. Controls. Add (LC );
Break ;
Case Datacontrolrowtype. datarow: // Data row
Linkbutton lbnfilename =   New Linkbutton (); // Add the linkbutton control and display it normally.
Lbnfilename. ID =   " LBN "   + Columnname;
Lbnfilename. databinding + =   New Eventhandler (lbnfilename_databinding ); // Data Binding
Lbnfilename. Click + =   New Eventhandler (lbnfilename_click ); // Click Event
Container. Controls. Add (lbnfilename );
// Guid
Label lblguid =   New Label (); // Add lable to hide
Lblguid. ID =   " Lblguid "   + Columnname;
Lblguid. databinding + =   New Eventhandler (lblguid_databinding );
Lblguid. Visible =   False ;
Container. Controls. Add (lblguid );
// Type
Label lbltype =   New Label (); // Add lable to hide
Lbltype. ID =   " Lbltype "   + Columnname;
Lbltype. databinding + =   New Eventhandler (lbltype_databinding );
Lbltype. Visible =   False ;
Container. Controls. Add (lbltype );
Break ;
Default :

Break ;
}
}

Void Lbltype_databinding ( Object Sender, eventargs E) // Lable Data Binding
{
Label LBL = (Label) sender;
Gridviewrow row = (Gridviewrow) LBL. namingcontainer;
LBL. Text = Databinder. eval (row. dataitem, " Lbltype "   + Columnname). tostring ();
}

Void Lblguid_databinding ( Object Sender, eventargs E) // Lable Data Binding

{
Label LBL = (Label) sender;
Gridviewrow row = (Gridviewrow) LBL. namingcontainer;
LBL. Text = Databinder. eval (row. dataitem, " Lblguid "   + Columnname). tostring ();

}

Void Lbnfilename_click ( Object Sender, eventargs E) // Click Event of the linkbutton, which is mainly used to download the document.
{
Try
{
Gridviewrow objgridviewrow = (Gridviewrow) (linkbutton) sender). namingcontainer;

String Strdqamanualfolder = Httpcontext. Current. server. mappath ( @" ~ \ Doc \ Manual " );
String Strfilename = (Linkbutton) objgridviewrow. findcontrol ( " LBN "   + Columnname). text;
String Strguid = (Label) objgridviewrow. findcontrol ( " Lblguid "   + Columnname). text;
String Strfiletype = (Label) objgridviewrow. findcontrol ( " Lbltype "   + Columnname). text;

Httpcontext. Current. response. clearheaders ();
Httpcontext. Current. response. contenttype =   " Application/octet-stream " ;
Httpcontext. Current. response. Clear ();
Httpcontext. Current. response. addheader ( " Content-Disposition " , " Attachment; filename = "   + System. Web. httputility. urlencode (strfilename, system. Text. encoding. utf7) +   "" );
Httpcontext. Current. response. transmitfile (strdqamanualfolder +   " \\ "   + Strguid +   ' . '   + Strfiletype );
}
Catch (Exception ex)
{
// Response. Write (ex. tostring ());
}
}

Void Lbnfilename_databinding ( Object Sender, eventargs E) // Linkbutton Data Binding
{
Linkbutton LBN = (Linkbutton) sender;
Gridviewrow row = (Gridviewrow) LBN. namingcontainer;
LBN. Text = Databinder. eval (row. dataitem, columnname). tostring ();
}

}

2. Add a gridview template Column

 

Code
1 Templatefield customfield;
2 Customfield =   New Templatefield ();
3 Customfield. headertemplate =   New Gridviewtemplate (datacontrolrowtype. header, listdesc );
4 Customfield. itemtemplate =   New Gridviewtemplate (datacontrolrowtype. datarow, column );
5 This . Gdvchnmanual. Columns. Add (customfield );
6
7 // You can add columns as needed.
8 This . Gdvchnmanual. datasource = DT;
9 This . Gdvchnmanual. databind ();

 

 

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.