views:

618

answers:

1

I am currently programming an ASP .NET FormView. The twist here is that I need to manually set the datasource via "FormView.DataSource = ". I added an ItemTemplate and added some form in the form view but even if the line of code that set the datasource view is called and the FormView.DataBind() is called, I still cannot see the data.

I then thought that probably the view is not in the item view or something. So I set the DefaultMode to edit and placed the whole code in the ItemEditTemplate but it still does not display the form when I pass thru the databind.

I know that doing this using the Datasource set in the aspx tags made it worked. But unfortunately my requirements is not to use the DataSource tag in asp .net but to do the binding manually.

Any ideas there or examples how to use FormView on manual databinding?

+1  A: 

Look at this code,

CUSTOM DataSource class,

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"&gt;

<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"&gt;
<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>
adatapost
I think I get the problem. What I did is just set the DataSource to a single dc.Student class and not a StudentList.
Nassign