Gridview主動排序功效的完成。本站提示廣大學習愛好者:(Gridview主動排序功效的完成)文章只能為提供參考,不一定能成為您想要的結果。以下是Gridview主動排序功效的完成正文
留意兩點:
1.要將gridview的AllowSorting屬性置為true,同時設置OnSorting事宜
2.在OnSorting事宜中對排序的列設定SortExpression屬性
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["Admin"] != "admin")
{
//假如會話過時,則應當從新登錄
this.Response.Write(" <script language=javascript>alert('你無權拜訪該頁面,請與治理員接洽!');window.location.href='../UserLogin.aspx';</script> ");
}
ViewState["sortExpression"] = "Isdistribution";
ViewState["sort"] = " ASC";
}
//綁定信息
BindNodeInfo();
}
public void BindNodeInfo()
{
NodeLogic log = new NodeLogic();
DataSet myset = log.GetNodeInfo(); //獲得數據源
DataView myview = myset.Tables[0].DefaultView;
myview.Sort = ViewState["sortExpression"].ToString() +" "+ ViewState["sort"].ToString();
this.NodeGridView.DataSource = myview;
NodeGridView.DataKeyNames = new string[] { "node_id" }; //設置主鍵字段
NodeGridView.DataBind(); //綁定GridView控件
}
protected void NodeGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.NodeGridView.PageIndex = e.NewPageIndex;
BindNodeInfo();
}
protected void NodeGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
// 主動給第一列編號
if (e.Row.RowIndex > -1)
{
e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);
}
}
protected void NodeGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
NodeLogic log = new NodeLogic();
int id = int.Parse(this.NodeGridView.DataKeys[e.RowIndex].Values[0].ToString());
if (log.DeleteNodeInfo(id))
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('刪除勝利!');", true);
}
else
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('刪除掉敗!');", true);
//從新更新數據顯示
BindNodeInfo();
}
protected void NodemGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void AddNode_Click(object sender, EventArgs e)
{
Response.Redirect("AddNode.aspx");
}
protected void NodeGridView_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["sortExpression"] != null)
{
if (ViewState["sort"].ToString() == "Asc")
{
ViewState["sort"] = "Desc";
}
else
{
ViewState["sort"] = "Asc";
}
}
BindNodeInfo();
}