关于获取GridView模版列绑定值的问题,还请高手指导.
我在绑定一个GridView的时候,里面添加了一个绑定列,并用Eval()方法进行了绑定
为什么我取不到它的值?自动绑定列的值却可以取到.谁能帮帮我啊。
<asp:GridView ID= "GridView1 " runat= "server " Width= "100% ">
<Columns>
<asp:TemplateField HeaderText= "123 ">
<ItemTemplate>
<%# Eval( "Weight ")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Label ID= "Label1 " runat= "server " Text= "Label "> </asp:Label>
protected void Page_Load(object sender, EventArgs e)
{
shopCart myCart = new shopCart();
DataTable dt = myCart.getMyCart(9);
this.GridView1.DataSource = dt.DefaultView;
this.GridView1.DataBind();
this.Label1.Text = this.GridView1.Rows[0].Cells[0].Text;
}
[解决办法]
GridView1.Rows[0].Cells[0]写法不适用于模板列
要使用 GridView1.Rows[0].FindControl( "x ")
例子
<%@ Page Language= "C# "%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN "
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "server ">
// 计算数据,完全可以从数据看取得
ICollection CreateDataSourceByXianhuiMeng()
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn( "学生班级 ", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn( "学生姓名 ", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn( "语文 ", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn( "数学 ", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn( "英语 ", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn( "计算机 ", typeof(System.Decimal)));
for (int i = 0; i < 8; i++)
{
System.Random rd = new System.Random(Environment.TickCount * i); ;
dr = dt.NewRow();
dr[0] = "班级 " + i.ToString();
dr[1] = "【孟子E章】 " + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = CreateDataSourceByXianhuiMeng();
GridView1.DataBind();
this.Label1.Text = this.GridView1.Rows[0].Cells[1].Text;
this.Label2.Text = ((Literal)this.GridView1.Rows[0].FindControl( "x ")).Text;
}
}
</script>
<html xmlns= "http://www.w3.org/1999/xhtml ">
<head id= "Head1 " runat= "server ">
</head>
<body>
<form id= "form1 " runat= "server ">
<asp:GridView ID= "GridView1 " runat= "server " Font-Size= "12px " CellPadding= "3 ">
<HeaderStyle BackColor= "#EDEDED " />
<Columns>
<asp:TemplateField HeaderText= "模版列 ">
<ItemTemplate>
<asp:Literal ID= "x " runat= "server " Text= ' <%#Eval( "学生姓名 ") %> ' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID= "Label1 " runat= "server " Text= "Label "> </asp:Label>
<asp:Label ID= "Label2 " runat= "server " Text= "Label "> </asp:Label>
</form>
</body>
</html>
[解决办法]
那你就往模板里中放一个控件不就得了吗
<ItemTemplate>
<asp:HyperLink ID= "HyperLink1 " runat= "server " Text= ' <%# DataBinder.Eval(Container.DataItem, "au_id ") %> '
</ItemTemplate>
不就得了 怎么简单怎么来啊 干什么非要怎么难怎么做啊,再说模板列就是用来放置控件用的