【问题标题】:Form View Manually Setting the DataSource Does not Show Template表单视图手动设置数据源不显示模板
【发布时间】:2010-11-27 03:39:59
【问题描述】:

我目前正在编写一个 ASP .NET FormView。这里的转折是我需要通过 "FormView.DataSource = " 手动设置数据源。我添加了一个 ItemTemplate 并在表单视图中添加了一些表单,但即使调用了设置数据源视图的代码行并调用了 FormView.DataBind(),我还是看不到数据。

然后我认为该视图可能不在项目视图中。所以我将 DefaultMode 设置为编辑并将整个代码放在 ItemEditTemplate 中,但是当我通过数据绑定时它仍然不显示表单。

我知道使用 aspx 标记中设置的数据源可以做到这一点。但不幸的是我的要求不是在asp .net中使用DataSource标签,而是手动进行绑定。

那里有任何想法或示例如何在手动数据绑定中使用 FormView?

【问题讨论】:

  • 可能你的数据源是空的!
  • 我也这么认为,但我的数据源是一个类。那行得通吗?它不是一个集合。只是一个数据类
  • FormView 数据源与任何其他控件的数据源相同。也许你做错了什么?

标签: asp.net data-binding formview mode


【解决方案1】:

看看这段代码,

自定义数据源类,

namespace dc
{
    public class Student
    {
        public int Roll { get; set; }
        public string Name { get; set; }
        public Student() { }
        public Student(int _roll, string _name)
        {
            Roll = _roll;
            Name = _name;
        }
    }
    public class StudentList : List<Student>
    {

    }
}

ASPX 标记,

<%@ 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">
    protected void Page_Load(object sender, EventArgs e)
    {
        dc.StudentList a = new dc.StudentList();
        a.Add(new dc.Student(1, "A"));
        a.Add(new dc.Student(2, "A"));

        FormView1.DataSource = a;
        FormView1.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Sample</title>
</head>
<body>
     <form id="form1" runat="server">
    <asp:FormView ID="FormView1" AllowPaging="true"  runat="server">
      <ItemTemplate>
          <asp:Label ID="Label1" runat="server" Text='<%# Eval("Roll") %>'></asp:Label>
          <asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
      </ItemTemplate>
    </asp:FormView>
    </form>
</body>
</html>

【讨论】:

  • 我想我明白了。我所做的只是将 DataSource 设置为单个 dc.Student 类而不是 StudentList。
  • 我仍然很难相信 ASP.NET Web 表单会强制您将列表绑定到 FormView(即显示单个记录,列表中的第一个对象).. 看图。
猜你喜欢
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 2021-02-14
  • 2018-01-04
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
  • 2012-11-13
相关资源
最近更新 更多