动态生成数个gridview Button得到隐藏ID列的值

 
-------SQL
  string sql = @"select ID,ProductName as '产品',CatelogID as '分值',UpdatedOn as '兑换点数'
FROM dbo.T_WAP_Product where CatelogID=@categoryID";
--------
protected void Page_Load(object sender, EventArgs e)
        {
            DataSet productDS = ProductDao.GetAllWapProducts();
            GridView productGV = null;
 
            for (int i = 0; i < productDS.Tables.Count; i++)
            {
                productGV = BindGridView(productDS.Tables[i]);
                productsTb.Controls.Add(productGV);
//如果需要隐藏ID列,一定要先bindgridview 再设置ID列不显示,不然拿不到ID
                productGV.Columns[0].Visible = false;
            }
 
        }
 
        private GridView BindGridView(DataTable dt)
        {
 
            Literal categoryNameLiteral = new Literal();
            categoryNameLiteral.Text = dt.TableName;
 
            GridView productGV = new GridView();
 
            productGV.AutoGenerateColumns = false;
            productGV.RowDataBound += new GridViewRowEventHandler(productGV_RowDataBound);
 
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                BoundField bc = new BoundField();
                bc.DataField = dt.Columns[i].ColumnName.ToString();
                bc.HeaderText = dt.Columns[i].Caption.ToString();
                productGV.Columns.Add(bc);
            }
 
            CommandField cfModify = new CommandField();  //绑定命令列
            cfModify.ButtonType = ButtonType.Button;
            cfModify.ControlStyle.CssClass = "input_sblue";
            cfModify.SelectText = "查看详情";
            cfModify.ShowSelectButton = true;
 
            productGV.Columns.Add(cfModify);
 
            productGV.DataSource = dt;
            productGV.DataBind();
 
            productsTb.Controls.Add(categoryNameLiteral);
 
            return productGV;
        }
 
        void productGV_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ((Button)(e.Row.Cells[4].Controls[0])).Attributes.Add("onclick", "javascript:test(this);return false;");
                ((Button)(e.Row.Cells[4].Controls[0])).Attributes.Add("name", e.Row.Cells[0].Text);
            }
        }
 
    private void SetCategoryNameStyle(string tableName)
    {
        Label categoryNameLbl = new Label();
        categoryNameLbl.Text = tableName;
 
        System.Drawing.ColorConverter colConvert = new ColorConverter();
        categoryNameLbl.BackColor = (System.Drawing.Color)colConvert.ConvertFromString("#0e93db");
 
        categoryNameLbl.CssClass = "tdStyle";
 
        productsTb.Controls.Add(categoryNameLbl);
    }
 
//前台JS

function test(o) {
//debugger
alert("ID"+o.name);
}

 
 CSS
.input_sblue{
      background:-webkit-gradient(linear, 0 0, 0 100%, from(#0061a8), to(#208bff));
      background:-moz-linear-gradient(top, #0061a8, #208bff);
      filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#0061a8, endColorstr=#208bff,grandientType=0);
      font-size:12px;
      color:#fff;
      font-weight:bold;
      padding:5px 5px;
      border-radius:5px;
      }
 

.mem_table3 tr td{ border:1px solid #97dbff;text-align:center; height:33px; padding:0 10px;}
.mem_table3{ margin:0 auto;}
.clr4 {
color:#fff;
}/*白色*/

 

转载于:https://www.cnblogs.com/Amity/p/3153371.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值