Example of dynamically adding a gridview template column and its value in ASP. NET 2.0

Source: Internet
Author: User

<%... @ Page Language = "C #" %>
<%... @ Import namespace = "system. Data" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>

<SCRIPT runat = "server">...
Icollection createdatasource ()
...{
Datatable dt = new datatable ();
Datarow Dr;
DT. Columns. Add (New datacolumn ("ID", typeof (int32 )));
DT. Columns. Add (New datacolumn ("text", typeof (string )));
For (INT I = 0; I <6; I ++)
...{
Dr = DT. newrow ();
Dr [0] = I;
Dr [1] = "list project" + I. tostring ();
DT. Rows. Add (DR );
}
Dataview DV = new dataview (DT );
Return DV;
}

Public class gridviewtemplate: itemplate
{
Private datacontrolrowtype templatetype;
Private string columnname;

Public gridviewtemplate (datacontrolrowtype type, string colname)
{
Templatetype = type;
Columnname = colname;
}

Public void instantiatein (system. Web. UI. Control container)
{
Switch (templatetype)
{
Case datacontrolrowtype. header:
Literal lc = new literal ();
LC. Text = columnname;
Container. Controls. Add (LC );
Break;
Case datacontrolrowtype. datarow:
Textbox TB = new Textbox ();
TB. ID = "itemnum ";
TB. width = 50;
TB. databinding + = new eventhandler (this. ondatabinding );
Container. Controls. Add (TB );
Break;
Default:
Break;
}
}
Public void ondatabinding (Object sender, eventargs E)
{
Textbox L = (textbox) sender; // textbox sends the Binding Request
Gridviewrow Container = (gridviewrow) L. namingcontainer;
L. Text = (datarowview) container. dataitem) ["itemnum"]. tostring (); // bind the itemnum Field
}
}

Protected void page_load (Object sender, eventargs E)
...{
If (! Ispostback)
...{
Templatefield customfield = new templatefield ();
Customfield. showheader = true;
Customfield. headertemplate = new gridviewtemplate (datacontrolrowtype. header, "dynamically add columns ");
Customfield. itemtemplate = new gridviewtemplate (datacontrolrowtype. datarow ,"");
Gridview1.columns. Add (customfield );
Gridview1.datasource = createdatasource ();
Gridview1.databind ();
}
}

Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
...{
If (E. Row. rowtype = datacontrolrowtype. datarow)
...{
// The values of other fields in the database can be accessed here. You can set the default options. The specific application depends on your use.
// The following is an example.
Datarowview GV = (datarowview) E. Row. dataitem;
Int itemseleted = int32.parse (GV. Row ["ID"]. tostring ()> 3? 0: int32.parse (GV. Row ["ID"]. tostring ());
Dropdownlist DR = (dropdownlist) E. Row. findcontrol ("dropdown ");
Dr. selectedindex = itemseleted;
}
}
</SCRIPT>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> example of dynamically adding a template column in The gridview </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false"
Onrowdatabound = "gridview1_rowdatabound">
<Columns>
<Asp: boundfield headertext = "title" datafield = "text"/>
</Columns>
</ASP: gridview>
</Form>
</Body>
</Html>

 

The dynamically added textbox is no longer available after PostBack. To obtain the value entered by the user, use request. Form [###].

#### The name attribute of the control on the client must be filled in

Fortunately, although the name of the textbox nested in the template column is automatically generated, the rule can follow:

Assume that the ID of the gridview control is gridview1 and that of the dynamically added Textbox Control is mytext:
Protected void button#click (Object sender, eventargs E)
{
For (INT I = 0; I <gridview1.rows. Count; I ++)
{
String txtname = "gridview1 $ CTL" + (I + 2). tostring (). padleft (2, '0') + "$ mytext ";
If (request. Form [txtname]! = NULL)
{
Response. Write (request. Form [txtname] + "<br/> ");
}
}
}

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.