动态生成数个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;
}/*白色*/