数据查询和显示

来源:互联网 发布:微信导入数据库失败 编辑:程序博客网 时间:2024/05/21 08:49

GridView

用编码绑定数据源

this.gridview.dataSource=UserManager.GetAllUsers

this.gridview.DataBind();

---------------------------------------------------------------------------------------------------

GridView中有很多模板

TemplateFile 可以代替其他模板的功能

ButtonField 通过CommandName来设置命令的名字

 

它的好处是:

  protected void GridView1_RowCommand(object sender,
        GridViewCommandEventArgs e)
    {
     
        int row = Convert.ToInt32( e.CommandArgument);
        if(e.CommandName=="toUpper")
            GridView1.Rows[row].Cells[1].Text = GridView1.Rows[row].Cells[1].Text.ToUpper();
        if(e.CommandName=="toLower")
            GridView1.Rows[row].Cells[1].Text = GridView1.Rows[row].Cells[1].Text.ToLower();
    }

 

RowCommand事件:当GridView内生成事件时触发

通过CommandName找到对应按钮要执行的事件

---------------------------------------------------------------------------------------------------------

RowDataBound在对行进行数据绑定后触发的事件

可以做特效

如:

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton button = e.Row.Cells[17].FindControl("LinkButton1") as LinkButton;
            button.CommandArgument = e.Row.RowIndex.ToString();//哪一行的LinkButton
            e.Row.Attributes.Add("onmouseover", "this.className='overrow'");
            if (e.Row.RowIndex % 2 == 0)
            {
                e.Row.Attributes.Add("class", "even");
                e.Row.Attributes.Add("onmouseout", "this.className='even'");
            }
            else
            {
                e.Row.CssClass = "odd";
                e.Row.Attributes.Add("onmouseout", "this.className='odd'");
            }

        }
    }

原创粉丝点击