<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyWeb._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>
<asp:GridView ID="gvMain" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" onrowdeleting="gvMain_RowDeleting"
onrowupdating="gvMain_RowUpdating"
onpageindexchanging="gvMain_PageIndexChanging"
onrowcancelingedit="gvMain_RowCancelingEdit"
onrowediting="gvMain_RowEditing" CellPadding="4" ForeColor="#333333"
GridLines="None" Height="198px" Width="638px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="id" HeaderText="编号" />
<asp:BoundField DataField="name" HeaderText="名称" />
<asp:BoundField DataField="psw" HeaderText="密码" />
<asp:CommandField ShowEditButton="True" HeaderText="编辑" />
<asp:CommandField ShowDeleteButton="True" HeaderText="删除" />
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</div>
</form>
</body>
</html>
private string connStr = "Server=127.0.0.1;Port=2001;User Id=SYSDBA;Password=123456;Database=WWW;Encoding=GBK;";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Bind();
}
}
//数据绑定的方法
public void Bind()
{
OscarConnection conn = new OscarConnection(connStr);
conn.Open();
string sql="SELECT * FROM A";
OscarCommand cmd = new OscarCommand(sql, conn);
OscarDataAdapter da = new OscarDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
DataTable user = ds.Tables[0];
this.gvMain.DataSource = user;
this.gvMain.AllowPaging = true;
this.gvMain.PageSize = 5;
this.gvMain.DataBind();
}
//删除的方法
public void DeleteA(int id)
{
OscarConnection conn = new OscarConnection(connStr);
conn.Open();
string sql="delete from A where id="+id;
OscarCommand cmd = new OscarCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
this.Response.Write("<script>alert('删除成功!');location.href='Default.aspx';</script>");
}
else
{
this.Response.Write("<script>alert('删除失败!');</script>");
}
}
//删除数据
protected void gvMain_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id =Convert.ToInt32(gvMain.DataKeys[e.RowIndex].Value);
if (id != 0)
{
DeleteA(id);
}
}
//更新数据
protected void gvMain_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(((TextBox)gvMain.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
string name = ((TextBox)gvMain.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
string psw = ((TextBox)gvMain.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
OscarConnection conn = new OscarConnection(connStr);
conn.Open();
string sql = "UPDATE A SET NAME='" + name + "', PSW='" + psw + "' WHERE ID=" + id;
OscarCommand cmd = new OscarCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
this.Response.Write("<script>alert('更新成功!');location.href='Default.aspx';</script>");
}
else
{
this.Response.Write("<script>alert('更新失败!');</script>");
}
}
//取消编辑
protected void gvMain_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvMain.EditIndex = -1;
Bind();
}
//行编辑时
protected void gvMain_RowEditing(object sender, GridViewEditEventArgs e)
{
gvMain.EditIndex = e.NewEditIndex;
Bind();
}
//分页重新绑定数据
protected void gvMain_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvMain.PageIndex = e.NewPageIndex;
Bind();
}