Default. aspx page:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "GPS_Web.Default" %>
<! 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> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<! -- Several events that must be written in the GridView: onrowediting, onrowupdating, onrowcancelingedit, onrowdeleting --->
<Asp: GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False"
Onpageindexchanging = "GridView1_PageIndexChanging" onrowcancelingedit = "GridView1_RowCancelingEdit"
Onrowediting = "GridView1_RowEditing" onrowupdating = "GridView1_RowUpdating"
Onrowdeleting = "GridView1_RowDeleting"
Onselectedindexchanging = "GridView1_SelectedIndexChanging">
<Columns>
<Asp: TemplateField HeaderText = "no." Visible = "False">
<ItemTemplate>
<Asp: Label ID = "lblNum" runat = "server" Text = '<% # Eval ("Num") %>'> </asp: Label>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField HeaderText = "name">
<EditItemTemplate>
<Asp: TextBox ID = "txtTableName" runat = "server" Text = '<% # Eval ("TableName") %>'> </asp: TextBox>
</EditItemTemplate>
<ItemTemplate>
<Asp: Label ID = "Label2" runat = "server" Text = '<% # Eval ("TableName") %>'> </asp: Label>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField HeaderText = "Address">
<EditItemTemplate>
<Asp: TextBox ID = "txtTextName" runat = "server" Text = '<% # Eval ("TextName") %>'> </asp: TextBox>
</EditItemTemplate>
<ItemTemplate>
<Asp: Label ID = "Label3" runat = "server" Text = '<% # Eval ("TextName") %>'> </asp: Label>
</ItemTemplate>
</Asp: TemplateField>
<Asp: CommandField HeaderText = "status" ShowSelectButton = "True"/>
<Asp: CommandField HeaderText = "edit" ShowEditButton = "True"/>
<Asp: CommandField HeaderText = "delete" ShowDeleteButton = "True"/>
</Columns>
</Asp: GridView>
<Br/>
<Asp: Button ID = "btnAdd" runat = "server" onclick = "btnAdd_Click" Text = "add"/>
<Br/>
<Br/>
</Div>
</Form>
</Body>
</Html>
Default. aspx. cs Page code:
Using System;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using GPS_Web.ywpages.DAL;
Using System. Data;
Namespace GPS_Web
{
/// <Summary>
/// </Summary>
Public partial class Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
GridViewBind ();
}
}
Private void GridViewBind ()
{
String SQL = "select Num, TableName, TextName from dbo. GroupType_Demo ";
Try
{// Bind a data source
GridView1.DataSource = SqlHelper. ExecuteDataset (SqlHelper. GetConnSting (), CommandType. Text, SQL). Tables [0];
GridView1.DataBind ();
}
Catch (Exception ex ){}
}
Protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e. NewPageIndex;
GridViewBind ();
}
Protected void GridView1_RowCancelingEdit (object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex =-1;
GridViewBind ();
}
Protected void gridviewinclurowediting (object sender, GridViewEditEventArgs e)
{// The index of the edit item in the gridview is equal to the index of the clicked row.
GridView1.EditIndex = e. NewEditIndex;
GridViewBind ();
}
/// <Summary>
/// Modify the event
/// </Summary>
Protected void GridView1_RowUpdating (object sender, GridViewUpdateEventArgs e)
{// Retrieve the serial number
Int Num = Convert. ToInt32 (Label) GridView1.Rows [e. RowIndex]. FindControl ("lblNum"). Text. ToString ());
// Obtain the content of the modified value
String TableName = (TextBox) GridView1.Rows [e. RowIndex]. FindControl ("txtTableName"). Text;
String TextName = (TextBox) GridView1.Rows [e. RowIndex]. FindControl ("txtTextName"). Text;
// Update records
String SQL = string. format ("update dbo. groupType_Demo set TableName = '{0}', TextName = '{1}' where Num = {2} ", TableName, TextName, Num );
Try
{
Int I = SqlHelper. ExecuteNonQuery (SqlHelper. GetConnSting (), CommandType. Text, SQL );
If (I> 0)
{
ClientScript. RegisterStartupScript (ClientScript. GetType (), "myscript", "<script language = 'javascript '> alert (' modified successfully! ') </Script> ");
}
Else
{
ClientScript. RegisterStartupScript (ClientScript. GetType (), "myscript", "<script language = 'javascript '> alert ('modification failed! ') </Script> ");
}
}
Catch (Exception ex)
{
ClientScript. RegisterStartupScript (ClientScript. GetType (), "myscript", "<script language = 'javascript '> alert (' the operation is invalid! ') </Script> ");
}
GridView1.EditIndex =-1;
GridViewBind ();
}
/// <Summary>
/// Delete an event
/// </Summary>
Protected void GridView1_RowDeleting (object sender, GridViewDeleteEventArgs e)
{
// Retrieve the serial number
Int Num = Convert. ToInt32 (Label) GridView1.Rows [e. RowIndex]. FindControl ("lblNum"). Text. ToString ());
// Update records
String SQL = string. Format ("delete dbo. GroupType_Demo where num = {0}", Num );
Try
{
Int I = SqlHelper. ExecuteNonQuery (SqlHelper. GetConnSting (), CommandType. Text, SQL );
If (I> 0)
{
ClientScript. RegisterStartupScript (ClientScript. GetType (), "myscript", "<script language = 'javascript '> alert ('deleted successfully! ') </Script> ");
}
Else
{
ClientScript. RegisterStartupScript (ClientScript. GetType (), "myscript", "<script language = 'javascript '> alert ('deletion failed! ') </Script> ");
}
}
Catch (Exception ex)
{
ClientScript. RegisterStartupScript (ClientScript. GetType (), "myscript", "<script language = 'javascript '> alert (' the operation is invalid! ') </Script> ");
}
GridView1.EditIndex =-1;
GridViewBind ();
}
/// <Summary>
/// Add button
/// </Summary>
Protected void btnAdd_Click (object sender, EventArgs e)
{
Response. Redirect ("~ /Default_Add.aspx ");
}
Protected void gridviewincluselectedindexchanging (object sender, GridViewSelectEventArgs e)
{
// Row number
Int I = e. NewSelectedIndex;
GridViewRow row = GridView1.Rows [e. NewSelectedIndex];
BtnAdd. Text = "you selected the row" + (I + 1) +. ";
}
}
}
Page effect after execution: