Previously, datasource was used to bind data. In this way, the development speed is very fast at the beginning. However, it takes a long time to discover its limits. Therefore, you can use Handwriting Editing instead. However, I wouldn't even write it at once. Each time it is said: The gridview "gridview1" triggers the unprocessed event "rowediting ".
It's really depressing. In CS, the gridview1_rowediting event is also written. I read it online and it seems that it is useless. Later, I found in an article that a few events were added to the gridview control on the aspx file to trigger the gridview1_rowediting function. -- Alas, the Javascript syntax is so similar that I ignored it. The event you click to edit is onrowediting, the "cancel" event is onrowcancelingedit, the "Update" event is onrowupdating, and the "delete" event is onrowdeleting. As for the event function, you can just give it a name. However, for the sake of normalization, gridview _... is used. Alas, I used to know this thing when I was learning VB.net. No.
The following is the online code:
The collected ASP. NET [C #] uses the gridview pagination, editing, deleting, and exporting the complete XML page. It is easy to use and can be used after modification.
ASPX page code:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "manage_news_default" enableeventvalidation = "false" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" cellpadding = "3" onrowdeleting = "gridview1_rowdeleting" onrowediting = "gridview1_rowediting"
Onrowupdating = "gridview1_rowupdating" onrowcancelingedit = "gridview1_rowcancelingedit" allowpaging = "true"
Onpageindexchanging = "gridview1_pageindexchanging" backcolor = "white" bordercolor = "# cccccc" borderstyle = "NONE" borderwidth = "1px"
>
<Footerstyle backcolor = "white" forecolor = "#000066"/>
<Rowstyle forecolor = "#000066" type = "regxph" text = "yourobjectname"/>
<Selectedrowstyle backcolor = "#669999" font-bold = "true" forecolor = "white"/>
<Pagerstyle backcolor = "white" forecolor = "#000066" horizontalalign = "Left"/>
<Headerstyle backcolor = "#006699" font-bold = "true" forecolor = "white"/>
<Columns>
<Asp: boundfield datafield = "log_id" headertext = "no." sortexpression = "log_id" insertvisible = "false" readonly = "true"/>
<Asp: boundfield datafield = "log_title" headertext = "title" sortexpression = "log_title"/>
<Asp: commandfield headertext = "edit" showeditbutton = "true"/>
<Asp: commandfield headertext = "delete" showdeletebutton = "true"/>
</Columns>
</ASP: gridview>
<Div> <asp: button id = "button1" runat = "server" text = "Export excel" onclick = "button#click"/> </div>
</Div>
</Form>
</Body>
</Html>
Aspx. CS code:
Using system;
Using system. Data;
Using system. Data. oledb;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Public partial class manage_news_default: system. Web. UI. Page
{
Oledbconnection sqlcon;
Oledbcommand sqlcom;
String strcon = configurationmanager. etettings ["DB"]. tostring ();
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
BIND ();
}
}
Protected void gridviewinclurowediting (Object sender, gridviewediteventargs E)
{
Gridview1.editindex = E. neweditindex;
BIND ();
}
// Delete
Protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs E)
{
String sqlstr = "delete from blog_article where log_id =" + convert. toint16 (gridview1.datakeys [E. rowindex]. value. tostring () + "";
Sqlcon = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath (strcon ));
Sqlcom = new oledbcommand (sqlstr, sqlcon );
Sqlcon. open ();
Sqlcom. executenonquery ();
Sqlcon. Close ();
BIND ();
}
// Update
Protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E)
{
Sqlcon = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath (strcon ));
String sqlstr = "Update blog_article set log_title = '"
+ (Textbox) (gridview1.rows [E. rowindex]. cells [1]. controls [0]). Text. tostring (). Trim ()
// + "', Field 2 ='" + (textbox) (gridview1.rows [E. rowindex]. cells [2]. controls [0]). text. tostring (). trim ()
+ "'Where log_id =" + convert. toint16 (gridview1.datakeys [E. rowindex]. value. tostring () + "";
Sqlcom = new oledbcommand (sqlstr, sqlcon );
Sqlcon. open ();
Sqlcom. executenonquery ();
Sqlcon. Close ();
Gridview1.editindex =-1;
BIND ();
}
// Cancel
Protected void gridview1_rowcancelingedit (Object sender, gridviewcancelediteventargs E)
{
Gridview1.editindex =-1;
BIND ();
}
// Bind
Public void BIND ()
{
String sqlstr = "select * from [blog_article]";
Sqlcon = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source =" + server. mappath (strcon ));
Oledbdataadapter myda = new oledbdataadapter (sqlstr, sqlcon );
Dataset myds = new dataset ();
Sqlcon. open ();
Myda. Fill (myds, "table ");
Gridview1.datasource = myds;
Gridview1.datakeynames = new string [] {"log_id"}; // primary key
Gridview1.databind ();
Sqlcon. Close ();
Sqlcon. Dispose ();
}
Protected void gridview1_pageindexchanging (Object sender, gridviewpageeventargs E)
{
Gridview1.pageindex = E. newpageindex;
BIND ();
}
Public override void verifyrenderinginserverform (Control)
{}
Protected void button#click (Object sender, eventargs E)
{
Response. Clear ();
Response. Buffer = true;
Response. charset = "gb2312 ";
Response. appendheader ("content-disposition", "attachment?filename=excel.xls"); // convert it to another suffix and change it to the following
Response. contentencoding = system. Text. encoding. utf7; //; character set format
Response. contenttype = "application/MS-excel"; // convert to another type, such as Excel to word
Stringwriter ostringwriter = new stringwriter ();
Htmltextwriter ohtmltextwriter = new htmltextwriter (ostringwriter );
Gridview1.allowpaging = false;
Gridview1.columns [gridview1.columns. Count-1]. Visible = false; // deletion is not displayed.
Gridview1.columns [gridview1.columns. Count-2]. Visible = false; // edit not displayed
BIND ();
Gridview1.rendercontrol (ohtmltextwriter );
Response. Output. Write (ostringwriter. tostring ());
Response. Flush ();
Response. End ();
}
}