asp.net c# tutorials and examples code for web developers

How to use DropDownList DataBinding event in asp.net c#

DropDownList DataBinding Event
dropdownlist is an asp.net list web server control. it can be populate by data bind with various data source controls such as LinqDataSource, ObjectDataSource, SqlDataSource, ArrayList etc. dropdownlist control have many built in properties, methods and events to extend its capabilities. the DataBinding event of dropdownlist occurs when the dropdownlist control binds to a data source. web developers can write an event handler in script section for dropdownlist DataBinding event using its OnDataBinding property.

the following c# example source code demonstrate us how can we use dropdownlist DataBinding event is asp.net.
DropDownListOnDataBindingEvent.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>

<!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 Button1_Click(object sender, System.EventArgs e)
    {
        string[] colors = { "DarkOrchid", "DarkViolet", "DodgerBlue", "GhostWhite", "GreenYellow" };
        DropDownList1.DataSource = colors;
        DropDownList1.DataBind();
    }

    protected void DropDownList1_DataBinding(object sender, EventArgs e)
    {
        Label1.Text = "Choose your favorite color: ";
        Label1.ForeColor = System.Drawing.Color.DodgerBlue;
        Label1.Font.Name = "Comic Sans MS";
        Label1.Font.Italic = true;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use DropDownList DataBinding event in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:DarkOrchid; font-style:italic;">
            How to use DropDownList
            <br /> OnDataBinding event in asp.net
        </h2>
        <hr width="350" align="left" color="Orchid" />
        <asp:Label 
            ID="Label1" 
            runat="server"
            >
        </asp:Label>
        <asp:DropDownList 
            ID="DropDownList1"
            runat="server"
            ForeColor="SkyBlue"
            Font-Size="Large"
            OnDataBinding="DropDownList1_DataBinding"
            >
        </asp:DropDownList>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Populate DropDownList"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>








Related asp.net examples