GridView 使用笔记 - 后台部分

来源:互联网 发布:网络运营助理岗位职责 编辑:程序博客网 时间:2024/06/05 07:20

数据绑定:


 private void GridDataBind(){        SqlHelper db = new SqlHelper();        db.Open();        SqlCommand Cmd = new SqlCommand();        Cmd.Connection = db.con;        Cmd.CommandTimeout = 60;        Cmd.CommandType = CommandType.Text;string sql = "select ID ,USER ,USER_TYPE,USER_LAND,REAL_NAME,PHONENO,ACTIVED from MD_USER where USER is not null " Cmd.CommandText = sql;        SqlDataAdapter str = new SqlDataAdapter(Cmd);        DataTable table = new DataTable();        str.Fill(table);        this.GridView1.DataSource = table;        this.GridView1.DataBind();        db.Close();}


  
 GridView1.DataSource:负责与数据源的交互,将检索出来的数据绑定到GridView中显示

GridView1.DataBind:将数据源绑定到GridView控件.


 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e){string USER = this.GridView1.SelectedRow.Cells[1].Text);string USER_TYPE = this.GridView1.SelectedRow.Cells[2].Text);Response.Redirect("~/Page/UserUpdate.aspx?user=" + USER + "&usertype=" + USER_TYPE);}

Response.Redirect: 打开新建窗口,将用户重新定向义到另一页。

此处选择按钮事件的左右为,用户点击名为 修改 的选择类型按钮,后台自动处理跳转到修改页。

跳转时记录部分信息方便在新页面中进行默认显示。

新建页前台可以通过只读等属性限制修改基础信息

<asp:TextBox ID="TextBox1" runat="server" Enabled="False" ReadOnly="True"></asp:TextBox>

Enabled:只读不可修改,文本框背景变灰。

ReadOnly:只读不可修改。

新页面后台可以通过下面方法调用信息。

this.TextBox1.Text = Request.QueryString["user"].ToString();


 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)        {            GridView1.PageIndex = e.NewPageIndex;            GridDataBind();        }

此事件为翻页


 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)        {            if (e.Row.RowType == DataControlRowType.DataRow)            {                //高亮显示指定行                e.Row.Attributes.Add("onMouseOver", "Color=this.style.backgroundColor;this.style.backgroundColor='#FFF000'");                e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=Color;");            }            if (e.Row.RowType == DataControlRowType.DataRow)            {                //改变布尔类型值为文字 并且改变颜色                if (e.Row.Cells[6].Text == "True")                {                    e.Row.Cells[6].Text = "已启用";                    e.Row.Cells[6].ForeColor = System.Drawing.Color.Green;                }                if (e.Row.Cells[6].Text.ToString() == "False")                {                    e.Row.Cells[6].Text = "已锁定";                    e.Row.Cells[6].ForeColor = System.Drawing.Color.Red;                }            }        }

      此事件为绑定完毕后动态触发事件,事件1为鼠标所在行显示为特定颜色

      事件2为改变数据库中读取到的布尔值为汉字,并修改其显示颜色


 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e){int rowvalue = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text.Trim());....}

删除按钮事件的获取所选行某列的值得方法如上所示,这里取的是第0行 为数据库表中ID字段的值, ID字段可以设置显示

为隐藏绑定ID字段的目的只为了后续的取值和对数据库的操作比较方便, 推荐此方法


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e){     switch (e.CommandName)      {         case"Reset":              int row = Convert.ToInt32(e.CommandArgument);              int value = int.Parse(GridView1.Rows[row].Cells[0].Text.Trim());....         break; case"xxx":. ... break;         default: return;     }      

自定义按钮事件推荐用switch  case 来判断所选按钮的命令ID名

这里的所选列的某行字段值得方法又有些不同,详见6,7行.


注意:自定义按钮事件的优先级高于选择,删除,编辑等事件的优先级,所以会有限进入switch判断是否有符合的

完毕后后再从default 中return回去执行相关事件.

 


0 0