GridView 使用笔记 - 后台部分

这篇博客主要介绍了ASP.NET中GridView控件的数据绑定方法,如使用GridView1.DataBind进行数据源绑定。同时,详细讲解了后台处理用户点击‘修改’按钮时如何跳转到新页面,并传递数据以实现页面默认显示。在新页面中,通过设置TextBox的Enabled和ReadOnly属性实现字段的只读展示。此外,还提到了如何在后台获取和修改数据显示,以及自定义按钮事件的优先级处理。
摘要由CSDN通过智能技术生成

数据绑定:


 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回去执行相关事件.

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值