Dynamic Creation of templatefield in gridview

Source: Internet
Author: User

Some time today, we sorted out how to add and edit columns using CS code and dynamically add and edit templates. It is relatively easy to add and edit columns dynamically, that is

Templatefield TF = new templatefield (), but it is rare to dynamically Add the edit template item itemtemplate to the edit column. I found a lot of information online that day to find a solution. First, create a class inheritance template interface itemplate and use the constructor to return the template class. I don't have to talk about it anymore. Now I will give an example and read it clearly. This example continues the previous dynamic binding field column.

Create template class Cs: gvitemtemplate. CS

Public class gridviewitemtemplate: itemplate
{
Private string OBJ; // control object string to determine which control to create
Private datacontrolrowtype templatetype; // template of the current row (header, item)
Private string colname; // the character to be displayed in the control or the field column name bound to the data source
Public gridviewitemtemplate (datacontrolrowtype Rtype, string controlstr, string coln)
{
Templatetype = Rtype;
Colname = coln;
OBJ = controlstr;
}
Public gridviewitemtemplate (datacontrolrowtype Rtype, string coln)
{
Templatetype = Rtype;
Colname = coln;
OBJ = "textbox ";
}
Public void instantiatein (system. Web. UI. Control container)
{
Switch (templatetype)
{
Case datacontrolrowtype. header:
Literal headlt = new literal ();
Headlt. Text = colname;
Container. Controls. Add (headlt );
Break;

Case datacontrolrowtype. datarow:
Switch (obj. tolower ())
{
Case "htmlcheckbox ":
Htmlinputcheckbox cbkeys = new htmlinputcheckbox ();
Cbkeys. ID = "cb_keys ";
Cbkeys. databinding + = new eventhandler (this. checkboxdatabinding );
Container. Controls. Add (cbkeys );
Break;
Default:
Textbox mytextbox = new Textbox ();
Mytextbox. databinding + = new eventhandler (this. textboxdatabinding );
Container. Controls. Add (mytextbox );
Break;

}
Break;
Default:
Break;
}
}
Private void textboxdatabinding (Object sender, eventargs E)
{

Textbox mytextbox = (textbox) sender;
Gridviewrow ROW = (gridviewrow) mytextbox. namingcontainer;
Mytextbox. Text = system. Web. UI. databinder. eval (row. dataitem, colname). tostring ();

}
Private void checkboxdatabinding (Object sender, eventargs E)
{
Gridviewrow ROW = NULL;
Switch (obj. tolower ())
{
Case "htmlcheckbox ":
Htmlinputcheckbox hcbox = (htmlinputcheckbox) sender;
Row = (gridviewrow) hcbox. namingcontainer;
Hcbox. value = system. Web. UI. databinder. eval (row. dataitem, colname). tostring ();
Break;
Default:
Checkbox cbox = (checkbox) sender;
Row = (gridviewrow) cbox. namingcontainer;
Cbox. Text = system. Web. UI. databinder. eval (row. dataitem, colname). tostring ();
Break;
}

}
}

 

Use the CS code of the page of the gridview control: testgridview. aspx. CS

Void bindfields (INT tbid)
{
Templatefield TF = new templatefield ();
TF. itemtemplate = new gridviewitemtemplate (datacontrolrowtype. datarow, "htmlcheckbox", "datakey ");
TF. headertemplate = new gridviewitemtemplate (datacontrolrowtype. header, "htmlcheckbox ","");
TF. headerstyle. cssclass = "cbox ";
This. gv_data.columns.add (TF );

}

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.