首页 > 代码库 > asp的gridview
asp的gridview
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DBHelper;
public partial class student : System.Web.UI.Page
{
public string id;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["SortOrder"] = "stu_id";
ViewState["OrderDire"] = "ASC";
Bind();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from t_student where stu_id=‘" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "‘";
SqlHelper.ExecteNonQueryText(sqlstr, null);
Response.Write("<script>alert(‘删除成功!‘)</script>");
GridView1.EditIndex = -1;
Bind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sPage = e.SortExpression;
if (ViewState["SortOrder"].ToString() == sPage)
{
if (ViewState["OrderDire"].ToString() == "Desc")
{
ViewState["OrderDire"] = "ASC";
}
else
{
ViewState["OrderDire"] = "Desc";
}
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
Bind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
Bind();
}
public void Bind()
{
string sql = "select * from t_student where 1=1";
DataTableCollection dc = SqlHelper.GetTableText(sql, null);
DataView view = dc[0].DefaultView;
string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
view.Sort = sort;
this.GridView1.DataSource = view;
GridView1.DataKeyNames = new string[] { "stu_id" };//主键
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(id);
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox CheckBox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (CheckBox.Checked == true)
{
id=GridView1.Rows[i].Cells[0].Text.ToString().Trim();
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("student_detail.aspx?id="+id);
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Redirect("student_detail.aspx?stu_id=" + id);
}
}
asp的gridview