日期:2014-05-18  浏览次数:20406 次

前台数据绑定
ASP.NET2005中...
<%# DataBinder.Eval(Container.DataItem, "title")%> 
这里绑定数据集 title,
 这个title在数据表里的位置是,是第二个,
请问怎么用 2表示这个记录绑定公式...
 因为涉及不同的表,不同的字段名,..

------解决方案--------------------
后台程序:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataColumn c0 = new DataColumn("id");
DataColumn c1 = new DataColumn("name");
dt.Columns.Add(c0);
dt.Columns.Add(c1);
DataRow dr0 = dt.NewRow();
dr0[0] = 0;
dr0[1] = "jeff";
DataRow dr1 = dt.NewRow();
dr1[0] = 1;
dr1[1] = "nina";
dt.Rows.Add(dr0);
dt.Rows.Add(dr1);//以上只是模拟个数据源

this.GridView1.DataSource = dt.DefaultView;
this.GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView o = e.Row.DataItem as DataRowView;
Label l1 = e.Row.FindControl("Label1") as Label;
Label l2 = e.Row.FindControl("Label2") as Label;
l1.Text = o.Row[0].ToString();
l2.Text = o.Row[1].ToString();
}
}
前台页面:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate><asp:Label ID="Label1" runat="server" Text=""></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate><asp:Label ID="Label2" runat="server" Text=""></asp:Label></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>