Table Data Binding : DataList « ASP.net Controls « ASP.NET Tutorial






<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
    DataSet myDataSet = new DataSet();
    
    void Page_Load(object sender, EventArgs e)
    {
       string ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings["MSDEConnectString"]);
       string CommandText = "SELECT * FROM Book";
    
       SqlConnection myConnection = new SqlConnection(ConnectionString);
       SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
    
       SqlDataAdapter myAdapter = new SqlDataAdapter();
    
       myAdapter.SelectCommand = myCommand;
    
       try {
          myConnection.Open();
          myAdapter.Fill(myDataSet);
       } catch (Exception ex) {
          throw (ex);
       } finally {
          myConnection.Close();
       }
    
       DataGrid1.DataSource = myDataSet;
       DataGrid1.DataBind();
       DataList1.DataSource = myDataSet;
       DataList1.DataBind();
       Repeater1.DataSource = myDataSet;
       Repeater1.DataBind();
    }

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
    <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
    <asp:DataList id="DataList1" runat="server">
        <ItemTemplate>
            <asp:Label id="Label1" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "BookID") %>'></asp:Label> <asp:Label id="Label2" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "BookTitle")%>'></asp:Label> <asp:Label id="Label3" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "BookPublisherID")%>'></asp:Label> <asp:Label id="Label4" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "BookMainTopic") %>'></asp:Label>
        </ItemTemplate>
        <HeaderTemplate>
            Books
        </HeaderTemplate>
    </asp:DataList>
    <asp:Repeater id="Repeater1" runat="server">
        <ItemTemplate>
            <asp:Label id="Label5" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "BookID") %>'></asp:Label>
            <asp:Label id="Label6" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "BookTitle")%>'></asp:Label>
            <asp:Label id="Label7" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "BookPublisherID")%>'></asp:Label>
            <asp:Label id="Label8" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "BookMainTopic") %>'></asp:Label>
        </ItemTemplate>
        <HeaderTemplate>
            Books
        </HeaderTemplate>
    </asp:Repeater>
    </form>
</body>
</html>



File: Web.config

<configuration>
    <appSettings>
        <add key="MSDEConnectString" value="server=(local)\YourDatabase;database=Books;uid=YourID;pwd=letmein;" />
    </appSettings>
</configuration>








3.39.DataList
3.39.1.Bind Data in Access to ASP:DataList (VB.net)
3.39.2.Set SelectedItemStyle, SelectedItemTemplate, ItemTemplate for asp:DataList (VB.net)
3.39.3.Use OnItemCommand, OnEditCommand, OnCancelCommand, OnUpdateCommand, OnDeleteCommand for asp:DataList (VB.net)
3.39.4.Table Data Binding