ASP.net Repeater 不绑定值
我有一个中继器定义为
<asp:Repeater id="rep1" runat="server">
<ItemTemplate>
<%#Eval("name")%>
</ItemTemplate>
</asp:Repeater>
后面的代码是 为什么
try
{
SqlConnection xconn = new SqlConnection();
xconn.ConnectionString = @"Data Source=XXXXXX;Trusted_Connection=yes;database=master";
xconn.Open();
lbl1.Text = "Connected to SQL";
SqlCommand ycmd = new SqlCommand("select * from student",xconn);
SqlDataReader dr = ycmd.ExecuteReader();
cdcatalog.DataSource = dr;
cdcatalog.DataBind();
}
catch (Exception)
{
lbl1.Text= "Cannot connect to SQL";
}
它不绑定中继器中的数据?
I have a repeater defined as
<asp:Repeater id="rep1" runat="server">
<ItemTemplate>
<%#Eval("name")%>
</ItemTemplate>
</asp:Repeater>
The code behind is as
try
{
SqlConnection xconn = new SqlConnection();
xconn.ConnectionString = @"Data Source=XXXXXX;Trusted_Connection=yes;database=master";
xconn.Open();
lbl1.Text = "Connected to SQL";
SqlCommand ycmd = new SqlCommand("select * from student",xconn);
SqlDataReader dr = ycmd.ExecuteReader();
cdcatalog.DataSource = dr;
cdcatalog.DataBind();
}
catch (Exception)
{
lbl1.Text= "Cannot connect to SQL";
}
Why does it not bind the data in the repeater?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要将数据读取器绑定到中继器?我建议您使用强类型对象。因此,首先定义一个代表您的数据的模型:
然后是一个获取这些学生的方法:
然后绑定中继器:
并在视图中:
还要注意,中继器的名称是
rep1
所以这就是您应该在后面的代码中使用什么。Why are you binding data readers to a repeater? I would recommend you using strongly typed objects. So start by defining a model that will represent your data:
then a method to fetch those students:
and then bind the repeater:
and in the view:
Also note that the name of the repeater is
rep1
so that's what you should use in your code behind.您的中继器的 ID 是
rep1
,而您正在数据绑定cdcatalog
。我想你的问题就在那里。这个cdcatalog
是什么?the ID of your repeater is
rep1
whereas you are databindingcdcatalog
. I guess your problem is there. What is thiscdcatalog
?