List based data binding : Databinding « Development « ASP.NET Tutorial






File: Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="ListDataBinding" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox id="MyListBox" 
                     runat="server" 
                     Width="200px" 
                     Height="200px"></asp:ListBox>
        <br /><br />
        <select id="MyHTMLSelect" size="1" runat="server"/>
    <br /><br />
      <asp:DropDownList id="MyDropDownListBox" 
                        runat="server" 
                        Width="248px" 
                        Height="22px"></asp:DropDownList>
      <br /><br />
      <asp:CheckBoxList id="MyCheckBoxList"  
                        runat="server" 
                        Width="201px" 
                        Height="157px"></asp:CheckBoxList>
      <br /><br />
      <asp:RadioButtonList id="MyRadioButtonList" 
                           runat="server" 
                           Width="249px" 
                           Height="158px"></asp:RadioButtonList>
        
     </div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class ListDataBinding : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<string> fruit = new List<string>();
        fruit.Add("A");
        fruit.Add("B");
        fruit.Add("C");
        fruit.Add("D");
        fruit.Add("E");
        fruit.Add("F");
        fruit.Add("G");
        fruit.Add("H");

        MyListBox.DataSource = fruit;
        MyDropDownListBox.DataSource = fruit;
        MyHTMLSelect.DataSource = fruit;
        MyCheckBoxList.DataSource = fruit;
        MyRadioButtonList.DataSource = fruit;

        this.DataBind();
    }
}








9.10.Databinding
9.10.1.Single-value data binding is a different approach to dynamic text.
9.10.2.Bind URL with property
9.10.3.List based data binding
9.10.4.Map structure based data binding