zoukankan      html  css  js  c++  java
  • asp.net数据控件 数据绑定控件比较

    数据绑定控件比较 (Reapter\DataList\GridView\DatailsView\FormView):
    1.插入功能方面:
    DetailsView和FormView具有插入功能,其它控件没有
    2.模板
    DataList\FormView\Repeater三种必须编辑模板,而
    GridView和DetailsView只有在将列转换成模板列以后才会出现各种模板.
    3.自动分页功能
    GridView ,DetailsView和FormView都是2.0版本新增控件,内置了分页,排序等等功能,
    其他需要手工定义
    4.数据呈现方式:
    GridView,DataList,Repeator用于呈现多列数据,
    DetailsView,FormView用于呈现单列数据,即常用的数据明细.


    DataList和Reapter都需要编辑模板列,而在模板列当中可以添加TextBox,同时可以指定TextBox的ID从而实现提取用户输入的 值,但是DataGrid和GridView两个件是不需要编辑模板的,它的编辑功能是自动生成的我们无法知道那些文本框的ID,也就无法通过ID来获取 用户的输入,那么可以通过对单元格的引用来实现:
    private void DataGrid1_UpdateCommand(object source,xx)
    {
        string bkid=DataGrid1.DataKeys[e.Item.ItemIndex].toString();//提取主键
        string bktitle=((TextBox)e.Item.Cells[1].Controls[0]).Text;//提取用户的输入
    }

    一.进入编辑状态:
    DataList1.EditItemIndex = e.Item.ItemIndex;
    DataGrid1.EditItemIndex = e.Item.ItemIndex;
    GridView1.EditIndex = e.NewEditIndex;
    DetailsView1.ChangeMode(DetailsViewMode.Edit);//进入编辑状态
    DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);//退出编辑状态


    二.设置主键:
    DataList1.DataKeyField = "bkid";
    DataGrid1.DataKeyField = "bkid";

    string[] str={"bkid"};
    GridView1.DataKeyNames = str;


    三.提取主键:
    string bkid = DataList1.DataKeys[e.Item.ItemIndex].ToString();//DataList
    string bkid = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();//DataGrid
    string bkid = GridView1.DataKeys[e.RowIndex].Value.ToString();//GridView
    string bkid = DetailsView1.DataKey[0].ToString();

    四.查找控件:
    string bktitle = ((TextBox)e.Item.FindControl("txtTile")).Text;//DataList
    string bktitle = ((TextBox)e.Item.Cells[1].Controls[0]).Text;//DataGrid
    string bktitle = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
    string bktitle = ((TextBox)DetailsView1.Rows[1].Cells[1].Controls[0]).Text;


    注意查找控件有两种方法:(各数据绑定控件的都可以用下面两种方法进行查找)
    1.如果知道控件的ID可以用这种方法
    ((TextBox)e.Item.FindControl("txtTile")).Text;//这是查找
    2.如果不知道控件的ID可用这种方法
    ((TextBox)e.Item.Cells[1].Controls[0]).Text;//这是索引


    五.给删除按钮添加确认:
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
         {
             if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
             {
                 LinkButton lbtn =(LinkButton) e.Item.FindControl("lbtndelete");
                 lbtn.Attributes.Add("OnClick","return confirm(‘确定要删除吗?‘)");
             }
         }

    protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
         {
             if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
             {
                 LinkButton lbtn = (LinkButton)e.Item.Cells[3].Controls[0];
                 lbtn.Attributes.Add("OnClick","return confirm(‘确认删除?‘)");
             }
         }


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
         {
             if(e.Row.RowType== DataControlRowType.DataRow)
             {
                 string strid = e.Row.Cells[0].Text;//获取第一行的字段值;
                 e.Row.Cells[3].Attributes.Add("OnClick", "return confirm(‘确认删除\""+strid+"\"?‘)");
                 //用了两个转义符将第一列的值用引号括起来,注意转义符后面一个将不被解释,是直接放上去;

             }
         }

  • 相关阅读:
    切换某个窗口为当前窗口并显示在最前面---非置顶
    C语言算法-求两直线夹角计算公式
    Qt编译时MinGW去掉对gcc动态库的依赖
    解决不能从 WTL::CString 转换为 ATL::CSimpleString & 的问题
    gcc编译器对宽字符的识别
    4月10日学习笔记——jQuery选择器
    HTML5
    4月8日学习笔记(js基础)
    网站易用性2
    网站易用性
  • 原文地址:https://www.cnblogs.com/lizhao/p/1990489.html
Copyright © 2011-2022 走看看