- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>GridView控件使用</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing" DataKeyNames="EMPNO" OnSorting="GridView1_Sorting" AllowSorting="True" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound">
- <Columns>
- <asp:BoundField DataField="EMPNO" HeaderText="用户ID" ReadOnly="True" SortExpression="EMPNO" />
- <asp:BoundField DataField="ENAME" HeaderText="用户名称" SortExpression="ENAME" />
- <asp:BoundField DataField="JOB" HeaderText="工作" SortExpression="JOB" />
- <asp:BoundField DataField="SAL" HeaderText="薪水" SortExpression="SAL" />
- <asp:BoundField DataField="COMM" HeaderText="奖金" SortExpression="COMM" />
- <asp:CommandField ShowSelectButton="True" HeaderText="选择" />
- <asp:CommandField ShowEditButton="True" HeaderText="编辑" />
- <asp:CommandField ShowDeleteButton="True" HeaderText="删除" />
- </Columns>
- <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
- <RowStyle BackColor="#E3EAEB" />
- <EditRowStyle BackColor="#7C6F57" />
- <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
- <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
- <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
- <AlternatingRowStyle BackColor="White" />
- </asp:GridView>
- </div>
- </form>
- </body>
- </html>
后台:
- using System;
- using System.Data;
- using System.Configuration;
- 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.Data.OracleClient;
- public partial class _Default : System.Web.UI.Page
- {
- protected string ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
- protected OracleConnection Connection;
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- ViewState["SortOrder"] = "EMPNO";
- ViewState["OrderDire"] = "ASC";
- Bind();
- }
- //删除数据时确认
- //for (int i = 0; i < GridView1.Rows.Count; i++)
- //{
- // ((LinkButton)GridView1.Rows[i].Cells[7].Controls[0]).Attributes.Add("onClick", "javascript:if(confirm('你真要删除选中的记录吗?')!=1){return false}");
- //}
- }
- protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)//取消
- {
- GridView1.EditIndex = -1;
- Bind();
- }
- protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)//删除
- {
- string sqlstr = "Delete From emp Where empno='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
- Connection = new OracleConnection(ConnectionString);
- OracleCommand cmd = new OracleCommand(sqlstr, Connection);
- Connection.Open();
- cmd.ExecuteNonQuery();
- Connection.Close();
- Bind();
- }
- protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)//编辑
- {
- GridView1.EditIndex = e.NewEditIndex;
- Bind();
- }
- protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)//更新
- {
- Connection = new OracleConnection(ConnectionString);
- string sqlstr = "Update emp set ename='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()
- + "',job='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim()
- + "',sal='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim()
- + "',comm='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim()
- + "' Where empno='" + GridView1.Rows[e.RowIndex].Cells[0].Text.ToString().Trim() + "'";
- //+ "' Where empno='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
- OracleCommand cmd = new OracleCommand();
- cmd.Connection = Connection;
- cmd.CommandText = sqlstr;
- Connection.Open();
- cmd.ExecuteNonQuery();
- Connection.Close();
- GridView1.EditIndex = -1;
- Bind();
- }
- protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)//排序
- {
- string sPage = e.SortExpression;
- if (sPage == ViewState["SortOrder"].ToString())
- {
- if (ViewState["OrderDire"].ToString() == "ASC")
- {
- ViewState["OrderDire"] = "DESC";
- }
- else
- {
- ViewState["OrderDire"] = "ASC";
- }
- }
- else
- {
- ViewState["SortOrder"] = e.SortExpression;
- }
- Bind();
- }
- public void Bind()//数据绑定
- {
- Connection = new OracleConnection(ConnectionString);
- string sqlstr = "Select empno,ename,job,sal,comm from emp order by " + ViewState["SortOrder"].ToString() + " " + ViewState["OrderDire"].ToString();
- OracleCommand cmd = new OracleCommand(sqlstr, Connection);
- OracleDataAdapter da = new OracleDataAdapter(cmd);
- DataSet ds = new DataSet();
- Connection.Open();
- da.Fill(ds, "用户表");
- Connection.Close();
- GridView1.PageSize = 6;
- GridView1.DataSource = ds.Tables["用户表"];
- GridView1.DataBind();
- }
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)//删除数据时确认
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
- {
- //((LinkButton)e.Row.Cells[7].Controls[0]).Attributes.Add("onClick", "javascript:return confirm('你真要删除用户编号为:/"" + e.Row.Cells[0].Text + "/"的记录吗?')");
- (e.Row.Cells[7]).Attributes.Add("onClick", "javascript:return confirm('你真要删除用户编号为:/"" + e.Row.Cells[0].Text + "/"的记录吗?')");
- }
- }
- }
- protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)//分页
- {
- GridView1.PageIndex = e.NewPageIndex;
- Bind();
- }
- }