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();
}
上一篇: web.config配置连接字符串的方法
下一篇: C#类中属性与成员变量的使用小结