zoukankan      html  css  js  c++  java
  • GridView的操作<1>:基本操作(行和单元格绑定)

    给GridView绑定基本数据:
     1protected void Page_Load(object sender, EventArgs e)
     2{
     3if (!IsPostBack)
     4{
     5GridView_DataBind();
     6}

     7}

     8private void GridView_DataBind()
     9{
    10DataTable DT = new DataTable();
    11string DBString =
    12"select account_phone,account_goods from t_account_all";
    13DT = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text,DBString);
    14Click_GridView.DataSource = DT;
    < /span>15< span style="color: #000000;">
    //这里要绑定的GridView 是:Click_GridView
    16Click_GridView.DataBind();
    17}
    给GridView的行和单元格绑定数据,在RowDataBound事件中进行,对于需要根据具体数据值绑定的时候效果最好:
     1protected void Click_GridView_RowDataBound(object sender, GridViewRowEventArgs e)
     2{
     3if ((e.Row.RowIndex != -1&& (e.Row.RowType == DataControlRowType.DataRow))
     4{
    < /span> 5 /* 单元格绑定:
     6< span style="color: #008000;">* 绑定属性onclick
    < span style="color: #008080;user-select:none;"> 7
    * 如果值为self.location那么表示在当前窗口打开新页面< br /> & nbsp;8* 如果值为window.open那么表示在新的窗口打开页面
     9*/
     
    10e.Row.Cells[0].Attributes.Add("onclick"/*"self.location='"*/"window.open('" + "../Default.aspx?" + HttpUtility.UrlEncode("Phone=" +                ((HyperLink)(e.Row.Cells[0].Controls[1])).Text+"')"));
    11e.Row.Cells[0].Attributes.Add("onmouseover""this.style.background='#f70'");
    12e.Row.Cells[0].Attributes.Add("onmouseout""this.style.background='#fff'");
    < /span>13< span style="color: #000000;">
    /* 行级绑定:
    < span style="color: #008080;user-select:none;">14
    * 点击行就执行跳转
    15*/

    16e.Row.Attributes.Add("onclick""window.open('../Default.aspx?" + HttpUtility.UrlEncode("Id=" + ((HyperLink)(e.Row.Cells[1].Controls[1])).Text) + "')");
    < /span>17< span style="color: #000000;">
    /* 在行级和单元格级同时绑定
    18* 在点击单元格1时,
    < span style="color: #008080;user-select:none;">19
    * 同时激发两个事件
    < span style="color: #008080;user-select:none;">20* 其中,单元格自己的事件在前
    21* 行级事件在后
    22* 两者不发生冲突
    23*/
    24
     1protected void DataBound_GridView_RowDataBound(object sender, GridViewRowEventArgs e)
     2{
    < /span> 3 实现1
    }
    25}
    GridView的RowDataBound另一个示例:
  • 相关阅读:
    LeetCode数学系列(1)——第172解题思路
    python的匿名函数lambda解释及用法
    LeetCode位操作系列(2)——位运算的常用技巧:lowbit运算,包含lowbit公式、讲解、231题运用
    【零散】jupyter notebook快捷键 mac版
    【油猴插件】分享推荐
    【Mac】 Chromedriver 存放路径
    【全网首发】微信公众号常见垃圾文章广告软文关键词整理
    Mac Chrome浏览器取消自动升级(最新版)
    requests与selenium之前cookies传递
    [转]scrapy中的request.meta
  • 原文地址:https://www.cnblogs.com/lixx/p/1185776.html
Copyright © 2011-2022 走看看