• 周三. 4月 24th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

gridview手写绑定

admin

11月 28, 2021

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Comp.MODEL;
using Comp.BLL;

namespace Comp.WEB.GridView
{
public partial class GridView : System.Web.UI.Page
{
//刚开始的时候下面没有别 所以点击 编辑列 增加几个 BoundField 并做相应的处理 (必须把下面的那个 自动生成列的钩 去掉)
//在 继续添加一个 增加删除更新 列 (也在编辑字段里)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}

}

void Bind()
{
Comp.BLL.Users bll = new BLL.Users();
IList<Comp.MODEL.Users> list = bll.GetList();
GridView2.DataSource = list;
GridView2.DataBind();

}

/// <summary>
/// 1点击编辑按钮的时候就会触发这个事件1
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//指示他是否编辑e.NewEditIndex.ToString();
GridView2.EditIndex = e.NewEditIndex;
this.Bind();

}

/// <summary>
/// 2当发生翻页的时候就会触发这个事件 ,在这个事件中 PageIndex 属于指示当前给用户显示哪一页
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView2.PageIndex = e.NewPageIndex;
this.Bind();

}
/// <summary>
/// 3点击取消 时候触犯的事件
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView2.EditIndex = -1;
this.Bind();

}
/// <summary>
/// 4更新时候 触发的事件
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

//1lab.Text = GridView2.Rows[0].Cells[0].Text; 或的某列 不处于编辑列 (文本形式)
//2 如果处于编辑状态 2中情况获得值
//2.1 GridView2.Rows[行的索引].Cells[列的索引]
// 2.2 ((TextBox)GridView2.Rows[0].Cells[2].Controls[0]).Text——获得普通的文本框
// ((CheckBox)GridView2.Rows[0].Cells[1].Controls[0]).Checked ——-获得选择框
Comp.BLL.Users bll = new BLL.Users();
//更新
//得到id
// string id= ((TextBox)GridView2.Rows[e.RowIndex].Cells[0].Controls[0]).Text; //此时的id 是可以更改的这样不行 怕用户更改id
// string id = GridView2.Rows[0].Cells[0].Text; //此时的id 是只读的文本框
//逻辑id显示出来不好所以一般隐藏 但是隐藏了就到不到id了 所以 要添加模板页 在模板页中添加 label 或隐藏域
//由于我们要更新所以在 编辑模板中方label2 别放错了 有很多模板的 EditItemTemplate 然后再label 中绑定id
//<EditItemTemplate>
// <asp:Label ID=”Label2″ runat=”server” Text='<%#Eval(“Id”) %>’></asp:Label>
// </EditItemTemplate>

//3 如果要获得模板中列的值必须通过FindControl 获得值 模板页可以隐藏但是可以获得值
string id = ((Label)GridView2.Rows[e.RowIndex].Cells[4].FindControl(“label2”)).Text;

string username = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
string pwd = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
//先从数据库中读取
Comp.MODEL.Users modal= bll.GetModel(Convert.ToInt32(id));
modal.UId=Convert.ToInt32(id);
modal.ULoginName =username;
modal.UPwd=pwd;

bll.Update(modal);
GridView2.EditIndex = -1;
Bind();

}
/// <summary>
/// 删除 删除的时候显示在itemplte 模板 所有在 Item模板中 方入labe 并绑定才能删除
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// //4 是第4列
string id = ((Label)GridView2.Rows[e.RowIndex].Cells[4].FindControl(“label3”)).Text;
Comp.BLL.Users bll = new BLL.Users();
bll.Del(id);
Bind();

}

protected void Button3_Click(object sender, EventArgs e)
{
List<string> list = new List<string>();

for (int i = 0; i < GridView2.Rows.Count; i++)
{
CheckBox chkbox = GridView2.Rows[i].FindControl(“CheckBox1”)as CheckBox;
//CheckBox chkbox=GridView2.Rows[i].Cells[0].FindControl(“CheckBox1”) as CheckBox

if (chkbox.Checked)
{
string id = chkbox.CssClass;
list.Add(id);
}

}
//if (list.Count == 0)
//{
// alert(请选择要选择的)
// return
//}

}
/// <summary>
/// 每行绑定是触发
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType== DataControlRowType.DataRow)
{

e.Row.Attributes.Add(“onmouseover”, “bgcolor=this.style.backgroundColor;this.style.backgroundColor=’yellow’;this.style.cursor=’pointer’;”);
e.Row.Attributes.Add(“onmouseout”, “this.style.backgroundColor=bgcolor;this.style.cursor=’default’;”);
//e.Row.Attributes.Add(“onclick”, “alert()”);

}

}
//当我们点击编辑按钮的时候 就会
// 在GridView中,当我们点击一个按钮,这个按钮的CommandName=
//Edit就会自动触发 RowEditing 的编辑事件.
//Cancel就会自动触发 RowCancelingEdit的取消事件.
//Update就会自动触发 RowUpdating的更新事件
//Delete就会自动触发 的删除事件RowDeleting

//不用 微软的编辑 更新 删除 用我们自己的标签是现实 更新 删除 图中的buttton

}
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注