Similar implementations are available in official fineui examples. In this example, columns are added dynamically, but sometimes we may need to modify columns dynamically. Let's take a look
Below is the code implementation
Dynamicgrid. aspx
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "dynamicgrid. aspx. CS "inherits =" fineuitest. dynamicgrid "%> <% @ register Assembly =" fineui "namespace =" fineui "tagprefix =" F "%> <! Doctype HTML> <HTML xmlns = "http://www.w3.org/1999/xhtml">
Dynamicgrid. aspx. CS
Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; using fineui; namespace fineuitest {public partial class dynamicgrid: system. web. UI. page {private const string grid_column_header = "grid_column_header"; private const string update_header = "update_header"; protected void page_load (Object sender, eventargs e) {If (! Ispostback) {initload () ;}} private void initload () {If (! Needupdateheader () {session. remove (grid_column_header); return;} List <string> headers = session [grid_column_header] As list <string>; If (headers = NULL) {return;} fineui. boundfield BF = NULL; foreach (string header in headers) {BF = new fineui. boundfield (); BF. headertext = header; BF. dataformatstring = "{0}"; griddemo. columns. add (BF) ;}} private bool needupdateheader () {If (request = NULL) {Return false;} string updateheader = request. querystring [update_header]; If (string. isnullorwhitespace (updateheader) | updateheader. toupper (). equals ("false") {return false;} return true;} protected void btnadd_click (Object sender, eventargs E) {list <string> headers = session [grid_column_header] As list <string>; If (headers = NULL) {headers = new list <string> ();} string header = "column" + Headers. count. tostring (); headers. add (header); Session [grid_column_header] = headers; pagecontext. redirect (fetchrefreshurl ();} private string fetchrefreshurl () {string url = "dynamicgrid. aspx "; Url + = "? "+ Update_header +" = true "; return URL;} protected void btnedit_click (Object sender, eventargs e) {list <string> headers = session [grid_column_header] As list <string>; if (headers = NULL | headers. count <= 0) {alert. show ("columns that cannot be modified"); return;} modify wedit. hidden = false; dropdownlistheaders. datasource = headers; dropdownlistheaders. databind (); dropdownlistheaders. selectedindex = 0;} protected void btnok_click (Object sender, eventargs e) {list <string> headers = session [grid_column_header] As list <string>; if (headers = NULL | headers. count <= 0) {alert. show ("columns that cannot be modified"); return;} If (string. isnullorwhitespace (textboxnewheader. text) {alert. show ("enter a new column name"); return;} int Index = dropdownlistheaders. selectedindex; headers [Index] = textboxnewheader. text; session [grid_column_header] = headers; pagecontext. registerstartupscript (activewindow. gethidereference (); pagecontext. redirect (fetchrefreshurl ();} protected void btncancel_click (Object sender, eventargs e) {pagecontext. registerstartupscript (activewindow. gethidereference ());}}}
Note:
1. The official example shows that the creation of dynamic columns is completed in page_load or page_init. To trigger this event, we can achieve this by redirecting redirect.
2. The column names created and modified are stored in the session.
3. The data in the table can also be implemented in a similar way. However, because the session cannot store too much data, the corresponding processing must be performed when the data volume is large. For example, you can use a database.
4. No matter how it is processed in the background, the code displayed on the front end can only be HTML and JS Code. Therefore, after the backend modifies the interface, it is necessary to refresh the interface. Some adopt direct refresh and some adopt asynchronous Ajax refresh. Therefore, we can modify the title of the interface and refresh the interface through redirection.
Reprinted please indicate the source http://blog.csdn.net/xxdddail/article/details/36378549