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 );
}