GridView Dynamically Create TemplateField

Source: Internet
Author: User

Today, some time, the use of CS code to add Edit columns and dynamic add Edit template has been sorted out. Dynamically adding edit columns is simpler, i.e.

TemplateField tf=new TemplateField (), but it is rare to edit column dynamically add edit Template items ItemTemplate. On the Internet that day to find a lot of information, only to find solutions. First, create a class to inherit the template interface ITemplate, and return the template class with the constructor implementation. No more nonsense to say, now give an example, see the clear. The example continues the previous dynamic bound field column.

Create a template class Cs:GVItemTemplate.cs

public class Gridviewitemtemplate:itemplate
{
private string obj; Control object to determine which control is specifically created
Private DataControlRowType Templatetype; Template for current row (Header,item)
private string colname; The character to display for the control, or the field column name of the bound 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
}

}
}

Page CS code using 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.