using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using Wuqi.Webdiyer;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
#region 绑定GridView
/// <summary>
/// 绑定GridView
/// </summary>
protected void BindGridView()
{
SqlConnection conn = DBConn.conn();
SqlDataAdapter per = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
// 总数
this.AspNetPager1.RecordCount = DataClass.CountNum("select count(*) from members");
// 查询内容
per.SelectCommand = new SqlCommand("select * from members", conn);
// 填充数据
per.Fill(ds, this.AspNetPager1.PageSize * (this.AspNetPager1.CurrentPageIndex - 1), this.AspNetPager1.PageSize, "members");
//ds.Tables["members"];
this.GridView1.DataSource = ds.Tables["members"];
this.GridView1.DataBind();
}
catch(SqlException e)
{
Response.Write(e.Errors.ToString());
}
finally
{
conn.Close();
per.Dispose();
ds.Dispose();
}
}
#endregion
#region 分页后绑定
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindGridView();
}
#endregion
#region 排序
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
#region // 判断排序方式
if (ViewState["Order"] == null)
{
ViewState["Order"] = "ASC";
}
else
{
if (ViewState["Order"].ToString() == "ASC")
{
ViewState["Order"] = "DESC";
}
else
{
ViewState["Order"] = "ASC";
}
}
#endregion
// 重新绑定
SqlConnection conn = DBConn.conn();
SqlDataAdapter per = new SqlDataAdapter();
DataSet ds = new DataSet();
try
{
per.SelectCommand = new SqlCommand("select * from members", conn);
// 总页数
this.AspNetPager1.RecordCount = DataClass.CountNum("select count(*) from members");
// 填充数据
per.Fill(ds,this.AspNetPager1.PageSize *(this.AspNetPager1.CurrentPageIndex - 1),this.AspNetPager1.PageSize, "members");
ds.Tables["members"].DefaultView.Sort = e.SortExpression + " " + ViewState["Order"].ToString();
this.GridView1.DataSource = ds.Tables["members"].DefaultView;
this.GridView1.DataBind();
}
catch
{
Response.Write("错误,捕获异常");
}
finally
{
conn.Close();
per.Dispose();
ds.Dispose();
}
}
#endregion
#region 删除
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string DelID = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
if (DataClass.DelFun("members", "uid = '" + DelID + "'"))
{
Jscript.Alert("删除成功!");
BindGridView();
}
else
{
Jscript.Alert("参数错误,请重试!");
}
}
#endregion
#region 变色
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//执行循环,保证每条数据都可以更新
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}
#endregion
#region 取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
BindGridView();
}
#endregion
#region 修改
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string UpdateID = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string[] Row = { "0", "1", "2", "3", "4", "5","6","7","8","9" };
for (Int32 i = 2; i < 8; i++)
{
Row[i] = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[i].Controls[0]).Text.ToString().Trim();
}
string SqlStr = "update members set userName = '"
+ Row[2] + "' , userPass = '"
+ Row[3] + "' , userAge = '"
+ Row[4] + "' , userSex = '"
+ Row[5] + "' , userCity = '"
+ Row[6] + "' , userEmail = '"
+ Row[7] + "' where uid = '"+UpdateID+"'";
SqlConnection conn = DBConn.conn();
SqlCommand cmd = new SqlCommand(SqlStr, conn);
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
Response.Write(ex.Errors.ToString());
}
finally
{
conn.Close();
cmd.Dispose();
}
this.GridView1.EditIndex = -1;
BindGridView();
}
#endregion
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
BindGridView();
}