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

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

我知道使用aspx标签中设置的数据源进行此操作使其工作。但不幸的是,我的要求不是在asp .net中使用DataSource标签,而是手动进行绑定。

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

有帮助吗?

解决方案

看看这段代码,

CUSTOM DataSource类,

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 Markup,

<%@ 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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top