日期:2014-05-17 浏览次数:20396 次
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="201px" Width="605px" AllowPaging="True" PageSize="20" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:TemplateField HeaderText="会员名" SortExpression="userid" > <ItemTemplate> <%#Eval("UserID")%> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="totalPoints" HeaderText="积分" /> <asp:TemplateField HeaderText="总积分"> <ItemTemplate> <% [color=#FF0000] string sql="select * from userinfo where userid='"+ (想得到<%#Eval("UserID")%>的值)+"'" [/color] %> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataBind(); } } //数据绑定方法 public void DataBind() { SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;User ID=sa;Password=123456"); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; con.Open(); cmd.CommandText = "select * from Tb1 "; DataTable dt = new DataTable(); dt.Load(cmd.ExecuteReader()); con.Close(); GridView1.DataSource = dt; GridView1.DataBind(); }
------解决方案--------------------
后台cs页面指定datasource。确定调用用了GridView的databind()方法。前台用<%# %>标记。再如楼上等人所说,绑定字段就成功啦。