ASP.net Repeater 不绑定值

发布于 2024-10-31 17:57:07 字数 790 浏览 0 评论 0原文

我有一个中继器定义为

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻梦旅人 2024-11-07 17:57:07

为什么要将数据读取器绑定到中继器?我建议您使用强类型对象。因此,首先定义一个代表您的数据的模型:

public class Student
{
    public string Name { get; set; }
}

然后是一个获取这些学生的方法:

public IEnumerable<Student> GetStudents()
{
    using (var conn = new SqlConnection("Data Source=XXXXXX;Trusted_Connection=yes;database=master"))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "SELECT Name FROM Students;";
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                yield return new Student
                {
                    Name = reader.GetString(reader.GetOrdinal("Name"));
                }
            }
        }
    }
}

然后绑定中继器:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        rep1.DataSource = GetStudents().ToArray();
        rep1.DataBind();
    }
}

并在视图中:

<asp:Repeater id="rep1" runat="server">
    <ItemTemplate>
        <%# Eval("Name") %>
    </ItemTemplate>
</asp:Repeater>

还要注意,中继器的名称是 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:

public class Student
{
    public string Name { get; set; }
}

then a method to fetch those students:

public IEnumerable<Student> GetStudents()
{
    using (var conn = new SqlConnection("Data Source=XXXXXX;Trusted_Connection=yes;database=master"))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = "SELECT Name FROM Students;";
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                yield return new Student
                {
                    Name = reader.GetString(reader.GetOrdinal("Name"));
                }
            }
        }
    }
}

and then bind the repeater:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        rep1.DataSource = GetStudents().ToArray();
        rep1.DataBind();
    }
}

and in the view:

<asp:Repeater id="rep1" runat="server">
    <ItemTemplate>
        <%# Eval("Name") %>
    </ItemTemplate>
</asp:Repeater>

Also note that the name of the repeater is rep1 so that's what you should use in your code behind.

橘虞初梦 2024-11-07 17:57:07

您的中继器的 ID 是 rep1,而您正在数据绑定 cdcatalog。我想你的问题就在那里。这个cdcatalog是什么?

the ID of your repeater is rep1 whereas you are databinding cdcatalog. I guess your problem is there. What is this cdcatalog?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文