Asp.net dynamically generates a gridview based on conditions and binds columns dynamically, and can be edited.

Source: Internet
Author: User

depending on your needs, a page contains several different buttons. You can click different buttons to generate a gridview in the same area below (of course, their gridview is different ). Previously, multiview was used for control. Several views were used to bind several gridview in HTML, and different views were switched Based on the buttons clicked. But I always think this method is uncomfortable ~~, There are too many HTML Code and it is very troublesome to control it ~!
this dynamic method is used for implementation. There is only one DIV in HTML, which is used to place the generated control, and all others are implemented in the background.
Note: If the control is added to HTML, the system processes the status and attributes of the control before and after the page is sent back. Therefore, when sending back, page lifecycle: reinitialize the page and load the control instance, load the control attributes and status in loadviewstate, and re-display the control status before sending back. However, for dynamically loaded controls, viewstate remembers their attributes and states, but does not store the controls themselves. So after sending back, the control will disappear ~
In view of this, there are multiple solutions, but in the final analysis, dynamic controls must be reloaded in the initialization status when the page is sent back. What I have done here is to reload it in the page_load event. Of course, it can also be done in earlier events.
Note: As there are several different loads, a sign (static string or attribute can be used) is made here ), in this way, you can determine which gridview to load when you reload it.
For more information, see the code:
HTML:



No title page









<%... -- place the dynamically generated gridview here -- %>




Cs:

Using system;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using system. Data. sqlclient;

Public partial class _ default: system. Web. UI. Page
...{
Gridview gvshow = NULL;
Public static string sign;

Protected void page_load (Object sender, eventargs E)
...{
If (sign! = NULL) // judge based on the markup symbol and re-bind the control
...{
If (Sign = "gv1 ")
Setbind ();
Else
Setbind2 ();
}
}

Public void setbind ()
...{
Datatable dt = getdatatable (); // obtain the data source

gvshow = new gridview ();
gvshow. width = unit. pixels (700);
gvshow. autogeneratecolumns = false;
gvshow. rowediting + = new gridviewediteventhandler (gvshow_rowediting); // Add an edit event
gvshow. rowdeleting + = new gridviewdeleteeventhandler (gvshow_rowdeleting);
gvshow. datasource = DT;

for (INT I = 0; I ... {
boundfield BC = new boundfield ();
BC. datafield = DT. columns [I]. columnname. tostring ();
BC. headertext = DT. columns [I]. caption. tostring ();
gvshow. columns. add (BC);
}

Commandfield cf = new commandfield ();
Cf. buttontype = buttontype. Link;
Cf. showeditbutton = true;
Cf. causesvalidation = false;
Gvshow. Columns. Add (CF );

Commandfield CF2 = new commandfield ();
Cf2.buttontype = buttontype. Link;
Cf2.showdeletebutton = true;
Cf2.causesvalidation = false;

Gvshow. Columns. Add (CF2 );

Gvshow. databind ();

Divshow. Controls. Add (gvshow );
}
Public void setbind2 ()
...{
Datatable dt = getdatatable (); // obtain the data source

Gvshow = new gridview ();
Gvshow. width = unit. pixel (700 );

Gvshow. autogeneratecolumns = false;
Gvshow. rowdeleting + = new gridviewdeleteeventhandler (gvshow_rowdeleting); // Add a delete event
Gvshow. datasource = DT;

For (INT I = 0; I <DT. Columns. Count; I ++)
...{
Boundfield BC = new boundfield ();
BC. datafield = DT. Columns [I]. columnname. tostring ();
BC. headertext = DT. Columns [I]. Caption. tostring ();
Gvshow. Columns. Add (BC );
}

Commandfield cf = new commandfield ();
Cf. buttontype = buttontype. Link;
Cf. showdeletebutton = true;
Cf. causesvalidation = false;

Gvshow. Columns. Add (CF );

Gvshow. databind ();

Divshow. Controls. Add (gvshow );
}

Void gvshow_rowdeleting (Object sender, gridviewdeleteeventargs E)
...{
Int I = E. rowindex;
Response. Write ("delete:" + gvshow. Rows [I]. cells [0]. Text. tostring ());
}
Protected void btngv_click (Object sender, eventargs e) // After selecting conditions, click Generate to generate a specific gridview
...{
Divshow. Controls. Clear ();
Setbind ();
Sign = "gv1 ";
}

Public void gvshow_rowediting (Object sender, gridviewediteventargs E)
...{
Int I = E. neweditindex;
String id = gvshow. Rows [I]. cells [0]. Text. tostring ();

Response. Write ("You want to edit" + id );
}

Public datatable getdatatable ()
...{
Dal. isdapp01 cdal = New DAL. isdapp01 ();
String SQL = "select * from test ";
Datatable dt = cdal. executequery (SQL). Tables [0];
Return DT;
}
Protected void button#click (Object sender, eventargs E)
...{
Divshow. Controls. Clear ();
Setbind2 ();
Sign = "gv2 ";
}
}

ArticleSource: http://www.diybl.com/course/4_webprogram/asp.net/netjs/2008520/117330.html

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.