.net之GridView、DataList、DetailsView(一)

 GridView:两种数据绑定方法
 方法一:得到数据后,赋给DataSource属性,然后执行控件的DataBind()方法。

BLL.Article bll = new BLL.Article();
List<MODEL.Article> list = bll.GetArticle();
GridView1.DataSource = list;
GridView1.DataBind();

 方法二:使用数据源控件,赋给数据绑定控件的DataSourceId。
 DataSource属性和DataSourceId不能同时使用。如果先用数据源控件获得数据并绑定显示,在程序中又需要改用手动绑定的方法显示数据,要先给DataSourceId赋null。
 列设置中可选字段的用法:
 BoundField:显示要绑定的数据
 CheckBoxField:以CheckBox显示,一般与数据库中bit类型绑定
 HyperLinkField:以超链接形式显示,一般链接地址与绑定数据有关系
 ImageField:以图片形式显示,一般图片地址与绑定数据有关系
 ButtonField:以按钮形式显示
 CommandField:包含能够触发增删改事件的按钮,其实就是一个普通按钮,不同的是CommandName的设置。
 在GridView控件中,一个按钮的CommandName属性为edit,cancel,update,delete时,则会自动触发GridView的编辑,取消,更新,删除功能,须实现方法,然后重新绑定数据。Page_Loda事件中要进行IsPostBack判断。
 EditIndex 属性:指用户正在编辑的行,如果没有编辑任何行,设置为-1。AllowPaging属性:是否自动分页。AutoGenerateColumns属性:是否自动生成列。PageIndex属性:当前页。PageSize属性:页大小。Rows属性:所显示数据行的集合

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }
    private void Bind()
    {
        BLL.Article bll = new BLL.Article();
        List<MODEL.Article> list = bll.GetArticle();
        GridView1.DataSource = list;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "a")
        {
            Response.Write("按钮1");
        }
        else if (e.CommandName == "b")
        {
            Response.Write("按钮2");
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Bind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Bind();
    }

 在GridView中获取用BoundFiled绑定的内容:GridView1.Rows[i].Cells[i].Text;处于编辑状态时获取用户输入的文本框中的值:(TextBox)GridView1.Rows[i].Cells[i].Controls[0].Text;如果用BoundFiled绑定的列被隐藏掉,那么隐藏的值无法获取到。用FindControl获取模版列中的值。

     //获取BoundFiled绑定字段的值
        Label2.Text = GridView1.Rows[0].Cells[0].Text;
        //获取编辑状态下字段的值
        CheckBox cb = (CheckBox)GridView1.Rows[0].Cells[1].Controls[0];
        Label2.Text = cb.Checked.ToString();
        //获取模板项中的值
        Label2.Text = ((Label)GridView1.Rows[0].Cells[9].FindControl("Label1")).Text;
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //获得修改后的值
        string Atitle = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
        bool IsDel = ((CheckBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Checked;
        //获得主键
        string Aid = ((Label)GridView1.Rows[e.RowIndex].Cells[9].FindControl("Label1")).Text;
        //根据主键获得实体类模型
        MODEL.Article model = new BLL.Article().GetSingleArticle(Aid);
        model.Aisdel = IsDel;
        model.Atitle = Atitle;//修改属性
        if (new BLL.Article().Edit(model) > 0)//如果编辑更新数据库成功
        {
            GridView1.EditIndex = -1;//取消编辑状态
            Bind();//重新绑定数据
        }
    }
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string Aid = ((Label)GridView1.Rows[e.RowIndex].Cells[9].FindControl("Label1")).Text;
        new BLL.Article().Del(Aid);
        Bind();
    }

转载于:https://www.cnblogs.com/huangshuhua/p/6275285.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值