Programmatic DataBinding in Asp.Net


An alternative to declarative databinding, you can programmatically bind any of the List controls to a data source

Example:
Lets create a BulletList which shows the list of Fruits.

<asp:BulletedList
            ID="blFruits"
            runat="server" />

And in Page_Load() event write this code. 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<String> fruits = new List<string>();
            fruits.Add("Apple");
            fruits.Add("Banana");
            fruits.Add("Mango");
            blFruits.DataSource = fruits;
            blFruits.DataBind();
        }
    }

if(!IsPostBack) prevents the re-initialisation of the BulletList items during postbacks.

Read About Declarative DataBinding : Declarative DataBinding in Asp.Net

1 comment: