How to DataBind ListView programmatically in asp.net

DataBind ListView programmatically
ListViewDataBind.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, EventArgs e)
    {
        ListView1.DataSourceID = "SqlDataSource1";
        ListView1.DataBind();
    }
    
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ListView DataBinding - how to DataBind ListView programmatically</title>
    <style type="text/css">
        .TableCSS
        {
            border-style:none;
            background-color:Navy;
            width: 700px;
            }
        .TableHeader
        {
            background-color:DarkBlue;
            color:Snow;
            font-size:large;
            font-family:Verdana;
            height:45px;
            text-align:center;
            }    
        .ItemCSS
        {
            background-color:DodgerBlue;
            color:Snow;
            font-family:MS Sans Serif;
            font-size:medium;
            font-weight:bold;
            height:28px;
            }  
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Crimson; font-style:italic;">ListView Example: How To DataBind ListView Programmatically</h2>
        <hr width="625" align="left" color="DarkRed" />
        <asp:SqlDataSource 
            ID="SqlDataSource1"
            runat="server"
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="Select Top 10 ProductName, UnitPrice, QuantityPerUnit From products Order By ProductName"
            >
        </asp:SqlDataSource>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            ForeColor="DodgerBlue"
            Text="Populate ListView"
            OnClick="Button1_Click"
            Font-Bold="true"
            Height="45"
            />
        <br /><br />            
        <asp:ListView 
            ID="ListView1"
            runat="server"
            >
            <LayoutTemplate>
                <table id="Table1" runat="server" class="TableCSS">
                    <tr id="Tr1" runat="server" class="TableHeader">
                        <td id="Td1" runat="server">Product Name</td>
                        <td id="Td2" runat="server">Unit Price</td>
                        <td id="Td3" runat="server">Quantity Per Unit</td>
                    </tr>
                    <tr id="ItemPlaceholder" runat="server">
                    </tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr class="ItemCSS">
                    <td>
                        <asp:Label 
                            ID="Label1"
                            runat="server"
                            Text='<%# Eval("ProductName")%>'
                            >
                        </asp:Label>
                    </td>
                    <td>
                        <asp:Label 
                            ID="Label2"
                            runat="server"
                            Text='<%# Eval("UnitPrice")%>'
                            >
                        </asp:Label>
                    </td>
                    <td>
                        <asp:Label 
                            ID="Label3"
                            runat="server"
                            Text='<%# Eval("QuantityPerUnit")%>'
                            >
                        </asp:Label>
                    </td>
                </tr>                
            </ItemTemplate>
        </asp:ListView>
    </div>
    </form>
</body>
</html>
More asp.net examples