ASP.NET Tutorial/ASP.net Controls/DataList — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:00, 26 мая 2010

Bind Data in Access to ASP:DataList (VB.net)

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
   sub Page_Load(Sender as Object, e as EventArgs) 
      dim objConn as new OleDbConnection( _
            "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
            & "DATA SOURCE=" _
            & Server.MapPath("EmployeeDatabase.mdb;"))
      dim objCmd as OleDbDataAdapter = new OleDbDataAdapter("select * from employee", objConn)
      dim ds as DataSet = new DataSet()
      objCmd.Fill(ds, "employee")
      MyDataList.DataSource = ds.Tables("employee").DefaultView
      MyDataList.DataBind()
    end sub
</script>
<html><body>
  <ASP:DataList id="MyDataList" RepeatColumns="2" 
     RepeatDirection="Vertical" runat="server">
     <ItemTemplate>
        <div style="padding:15,15,15,15;font-size:10pt;
           font-family:Verdana">
        <div style="font:12pt verdana;color:darkred">
           <i><b><%# DataBinder.Eval(Container.DataItem, "FirstName")%>&nbsp; 
           <%# DataBinder.Eval(Container.DataItem, "LastName")%>
           </i></b>
        </div>
        <br>
        <b>ID: </b><%# DataBinder.Eval(Container.DataItem, _
           "ID") %><br>
          </div>
      </ItemTemplate>
  </ASP:DataList>
</body></html>


Set SelectedItemStyle, SelectedItemTemplate, ItemTemplate for asp:DataList (VB.net)

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
   sub Page_Load(Sender as Object, e as EventArgs) 
      "set up connection
      dim myConnection as new OleDbConnection( _
            "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
            & "DATA SOURCE=" _
            & Server.MapPath("EmployeeDatabase.mdb;"))
      "open connection
      dim myCommand as new OleDbDataAdapter _
         ("select * from employee", myConnection)
      "fill dataset
      dim ds as DataSet = new DataSet()
      myCommand.Fill(ds, "employee")
      "select data view and bind to server control
      DataList1.DataSource = ds.Tables("employee").DefaultView
      DataBind()
   end sub
   
   sub DataList1_ItemCommand(Sender as object, e as _
      DataListCommandEventArgs)
      DataList1.SelectedIndex = e.Item.ItemIndex
      DataList1.DataBind()
   end sub
</script>
<html><body>
   <form runat="server">
   <asp:DataList id="DataList1" runat="server"
      SelectedItemStyle-BackColor="#cccc99"
      repeatlayout="table"
      repeatdirection="horizontal"
      OnItemCommand="DataList1_ItemCommand"
      DataKeyField="ID">
      <ItemTemplate>
         <asp:LinkButton id="button1" runat="server"
            Text="<%# Container.DataItem("FirstName") & " " & _
               Container.DataItem("LastName") %>"
            Command="select" />
         
      </ItemTemplate>
      <SelectedItemTemplate>
         <%# Container.DataItem("FirstName") & " " & _
            Container.DataItem("LastName") %><br>
         ID:
         <%# Container.DataItem("ID") %>
         <br>
      </SelectedItemTemplate>
   </asp:DataList>

   </form>
   
</body></html>


Table Data Binding

<%@ 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>


Use OnItemCommand, OnEditCommand, OnCancelCommand, OnUpdateCommand, OnDeleteCommand for asp:DataList (VB.net)

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
   sub Page_Load(Sender as Object, e as EventArgs) 
      dim myConnection as new OleDbConnection( _
            "PROVIDER=Microsoft.Jet.OLEDB.4.0;" _
            & "DATA SOURCE=" _
            & Server.MapPath("EmployeeDatabase.mdb;"))
      dim myCommand as new OleDbDataAdapter("select * from employee", myConnection)
      dim ds as DataSet = new DataSet()
      myCommand.Fill(ds, "employee")
      DataList1.DataSource = ds.Tables("employee").DefaultView
      DataBind()
   end sub
   
   sub DataList1_ItemCommand(Sender as object, e as DataListCommandEventArgs)
      DataList1.SelectedIndex = e.Item.ItemIndex
      DataList1.DataBind()
   end sub
   sub DataList1_EditCommand(Sender as object, e as DataListCommandEventArgs)
      DataList1.EditItemIndex = e.Item.ItemIndex
      DataList1.DataBind()
   end sub
   
   sub DataList1_CancelCommand(Sender as object, e as DataListCommandEventArgs)
      DataList1.EditItemIndex = -1
      DataList1.DataBind()
   end sub
   sub DataList1_UpdateCommand(Sender as object, e as DataListCommandEventArgs)
      DataList1.DataBind()
   end sub
   sub DataList1_DeleteCommand(Sender as object, e as DataListCommandEventArgs)
      DataList1.DataBind()
   end sub
</script>
<html><body>
   <form runat="server">
   <asp:DataList id="DataList1" runat="server"
      SelectedItemStyle-BackColor="#cccc99"
      repeatlayout="table"
      repeatdirection="horizontal"
      OnItemCommand="DataList1_ItemCommand"
      OnEditCommand="DataList1_EditCommand"
      OnCancelCommand="DataList1_CancelCommand"
      OnUpdateCommand="DataList1_UpdateCommand"
      OnDeleteCommand="DataList1_DeleteCommand"
      DataKeyField="ID">
      <ItemTemplate>
         <asp:LinkButton id="button1" runat="server"
            Text="<%# Container.DataItem("FirstName") & " " & _
               Container.DataItem("LastName") %>"
            CommandName="Edit" />
         
      </ItemTemplate>
      <SelectedItemTemplate>
         <%# Container.DataItem("FirstName") & " " & _
            Container.DataItem("LastName") %><br>
         ID:
         <%# Container.DataItem("ID") %>
         <br>
      </SelectedItemTemplate>
      
      <EditItemTemplate>
         <asp:LinkButton id="lbtCancel" runat="server"
            CommandName="Cancel"
            Text="Cancel" />
         <asp:LinkButton id="lbtUpdate" runat="server"
            CommandName="Update"
            Text="Update" />
         <asp:LinkButton id="lbtDelete" runat="server"
            CommandName="Delete"
            Text="Delete" />
      </EditItemTemplate>
   </asp:DataList>
   </form>
   
</body></html>