Bind DataList to Class Property (C#) : Bind a Property « Data Binding « ASP.Net

ASP.Net
1. ADO.net Database
2. Ajax
3. Asp Control
4. Collections
5. Components
6. Data Binding
7. Development
8. File Directory
9. HTML Control
10. Language Basics
11. Login Security
12. Mobile Control
13. Network
14. Page
15. Request
16. Response
17. Server
18. Session Cookie
19. Sitemap
20. Theme Style
21. User Control and Master Page
22. Validation by Control
23. Validation by Function
24. WebPart
25. WPF
26. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.Net » Data Binding » Bind a Property 
Bind DataList to Class Property (C#)

<%@ Page language="c#" src="GuestBook.aspx.cs" AutoEventWireup="false" Inherits="GuestBook.GuestBook" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <P>
        <asp:DataList id="GuestBookList" runat="server">
          <SelectedItemStyle></SelectedItemStyle>
          <ItemStyle></ItemStyle>
          <FooterStyle></FooterStyle>
          <ItemTemplate>
            Left By:
            <%# DataBinder.Eval(Container.DataItem, "Author"%>
            <br>
            <b>
              <%# DataBinder.Eval(Container.DataItem, "Message"%>
            </b>
            <br>
            Left On:
            <%# DataBinder.Eval(Container.DataItem, "Submitted"%>
          </ItemTemplate>
          <HeaderStyle></HeaderStyle>
        </asp:DataList>
        </P>
      <DIV style="WIDTH: 560px; POSITION: relative; HEIGHT: 248px" ms_positioning="GridLayout">
        <asp:Label id="Label1" runat="server" Width="104px" Height="24px">Your Name:</asp:Label>
        <asp:Label id="Label2" runat="server" Width="104px" Height="24px">Your Message:</asp:Label>
        <asp:TextBox id="txtName" runat="server" Width="392px"></asp:TextBox>
        <asp:TextBox id="txtMessage" runat="server" Width="392px" Height="154px" TextMode="MultiLine"></asp:TextBox>
        <asp:Button id="cmdSubmit" runat="server" Width="104px" Text="Submit"></asp:Button></DIV>
    </form>
  </body>
</HTML>


<%--
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

namespace GuestBook
{
  /// <summary>
  /// Summary description for GuestBook.
  /// </summary>
  public class GuestBook : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.DataList GuestBookList;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.TextBox txtName;
    protected System.Web.UI.WebControls.TextBox txtMessage;
    protected System.Web.UI.WebControls.Button cmdSubmit;

      private string guestBookName;

    private void Page_Load(object sender, System.EventArgs e)
    {
      guestBookName = Server.MapPath("GuestBook");

      if (!this.IsPostBack)
      {
        GuestBookList.DataSource = GetAllEntries();
        GuestBookList.DataBind();
      }

    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }
    
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
      this.cmdSubmit.Click += new System.EventHandler(this.cmdSubmit_Click);
      this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void cmdSubmit_Click(object sender, System.EventArgs e)
    {
      BookEntry newEntry = new BookEntry();
      newEntry.Author = txtName.Text;
      newEntry.Submitted = DateTime.Now;
      newEntry.Message = txtMessage.Text;

      SaveEntry(newEntry);

      GuestBookList.DataSource = GetAllEntries();
      GuestBookList.DataBind();

      txtName.Text = "";
      txtMessage.Text = "";
    }

    private ArrayList GetAllEntries()
    {
      ArrayList entries = new ArrayList();
      DirectoryInfo guestBookDir = new DirectoryInfo(guestBookName);

      foreach (FileInfo fileItem in guestBookDir.GetFiles()){
        entries.Add(GetEntryFromFile(fileItem));
      }

      return entries;
    }

    private BookEntry GetEntryFromFile(FileInfo entryFile)
    {
      BookEntry newEntry = new BookEntry();
      StreamReader r = entryFile.OpenText();
      newEntry.Author = r.ReadLine();
      newEntry.Submitted = DateTime.Parse(r.ReadLine());
      newEntry.Message = r.ReadLine();
      r.Close();

      return newEntry;
    }

    private void SaveEntry(BookEntry entry)
    {
      string fileName = guestBookName + @"\";
      fileName += DateTime.Now.Ticks.ToString();
      FileInfo newFile = new FileInfo(fileName);
      StreamWriter w = newFile.CreateText();
      w.WriteLine(entry.Author);
      w.WriteLine(entry.Submitted.ToString());
      w.WriteLine(entry.Message);
      w.Flush();
      w.Close();
    }

  }


  public class BookEntry
  {
    private string author;
    private DateTime submitted;
    private string message;

    public string Author
    {
      get
      { return author; }
      set
      { author = value; }
    }

    public DateTime Submitted
    {
      get
      { return submitted; }
      set
      { submitted = value; }
    }

    public string Message
    {
      get
      { return message; }
      set
      { message = value; }
    }
  }

}


--%>
           
       
Related examples in the same category
1. Bind a property Demo (VB.net)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.