GridView的一些技巧总结

这里记录下经常用到的三个方法
1.GridView_RowCreated

1protected void gridview_RowCreated(object sender, GridViewRowEventArgs e)
2{
3    if (e.Row.RowType == DataControlRowType.DataRow)
4    {
5       
6        LinkButton linkbutton = (LinkButton)e.Row.FindControl("lbtn");
7        linkbutton.CommandArgument = e.Row.RowIndex.ToString();
8    }

9}
2.GridView_RowDataBound
前提条件是在第0列放置一个visble为false的模版列,里面内容为Label,捆绑着表的ID
 1protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
 2{
 3    if (e.Row.RowType == DataControlRowType.DataRow)
 4    {
 5        LinkButton linkbutton = (LinkButton)e.Row.FindControl("lbtn");
 6        int id = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ID").ToString());
 7        //获取了ID后使用业务方法
 8        linkbutton.Text =     //指定模版列的控件
 9    }

10    //鼠标移动变颜色
11    for (int i = -1; i < GridView.Rows.Count; i++)    
12    {
13        //当鼠标停留时更改背景色        
             e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#B5C9DD'");
14        //当鼠标移开时还原背景色
15         e.Row.Attributes.Add("onmouseout""this.style.backgroundColor=c");
16    }

17}
3.GridView_RowCommand
前提条件是在第0列放置一个visble为false的模版列,里面内容为Label,捆绑着表的ID
1protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
2{
3    if (e.CommandName == "commandname")
4    {        
5        int index = Convert.ToInt32(e.CommandArgument);
6        GridViewRow row = GridView.Rows[index];
7        int id = Int32.Parse(((Label)row.FindControl("ID")).Text);
8    }

9}
posted @ 2007-12-12 16:50  CowboyRyan  阅读(377)  评论(1)    收藏  举报