ASP.NET Tutorial/Data Binding/DetailsView

Материал из .Net Framework эксперт
Версия от 11:58, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Bind a DetailsView control programmatically to a data source

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
    public class Employee
    {
        public string _firstName;
        public string _lastName;
        public bool _retired;
        public string FirstName
        {
            get { return _firstName; }
        }
        public string LastName
        {
            get { return _lastName; }
        }
        public bool Retired
        {
            get { return _retired; }
        }
        public Employee(string firstName, string lastName, bool retired)
        {
            _firstName = firstName;
            _lastName = lastName;
            _retired = retired;
        }
    }
    void Page_Load()
    {
        Employee newEmployee = new Employee("A", "B", false);
        List<Employee> employees = new List<Employee>();
        employees.Add(newEmployee);
        dtlProducts.DataSource = employees;
        dtlProducts.DataBind();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Employee</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DetailsView
        id="dtlProducts"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Customizing the Paging Interface

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Pager Settings</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DetailsView
        id="dtlProducts"
        DataSourceID="srcProducts"
        AllowPaging="true"
        Runat="server">
        <PagerSettings
            Mode="NextPreviousFirstLast"
            FirstPageText="[First Product]"
            LastPageText="[Last Product]"
            NextPageText="[Next Product]"
            PreviousPageText="[Previous Product]" />
    </asp:DetailsView>
    <asp:SqlDataSource
        id="srcProducts"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        SelectCommand="SELECT Id,Title,Director,InStock FROM Products"
        Runat="server" />
    </div>
    </form>
</body>
</html>
File: Web.config
<configuration>
  <connectionStrings>
    <add name="Products" 
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>


Displaying content when no results are returned.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DetailsView
        id="dtlProducts"
        DataSourceID="srcProducts"
        Runat="server">
        <EmptyDataTemplate>
        <div class="noMatch">
            <h1>No Matching Results!</h1>
            Please select a different record.
        </div>
        </EmptyDataTemplate>
    </asp:DetailsView>
    <asp:SqlDataSource
        id="srcProducts"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        SelectCommand="SELECT Id,Title,Director,InStock FROM Products
            WHERE Id=-1"
        Runat="server" />
    </div>
    </form>
</body>
</html>
File: Web.config
<configuration>
  <connectionStrings>
    <add name="Products" 
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>


Displaying Empty Data with the DetailsView Control

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Empty Data Text</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DetailsView
        id="dtlProducts"
        DataSourceID="srcProducts"
        EmptyDataText="<b>No Matching Record!</b>"
        Runat="server" />
    <asp:SqlDataSource
        id="srcProducts"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        SelectCommand="SELECT Id,Title,Director,InStock FROM Products
            WHERE Id=-1"
        Runat="server" />
    </div>
    </form>
</body>
</html>
File: Web.config
<configuration>
  <connectionStrings>
    <add name="Products" 
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>


Format DetailsView

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <style type="text/css">
        .products td,.products th
        {
            padding:10px;
        }
        .products
        {
            border:double 4px black;
        }
        .header
        {
            letter-spacing:8px;
            font:bold 16px Arial,Sans-Serif;
            background-color:silver;
        }
        .fieldHeader
        {
            font-weight:bold;
        }
        .alternating
        {
            background-color:#eeeeee;
        }
        .rumand
        {
            background-color:silver;
        }
        .rumand a
        {
            color:black;
            background-color:#eeeeee;
            font:14px Arials,Sans-Serif;
            text-decoration:none;
            padding:3px;
            border:solid 1px black;
        }
        .rumand a:hover
        {
            background-color:yellow;
        }
        .pager td
        {
            padding:2px;
        }
    </style>
    <title>Format DetailsView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DetailsView
        id="dtlProducts"
        DataSourceID="srcProducts"
        AutoGenerateInsertButton="true"
        AllowPaging="true"
        GridLines="None"
        HeaderText="Products"
        CssClass="products"
        HeaderStyle-CssClass="header"
        FieldHeaderStyle-CssClass="fieldHeader"
        AlternatingRowStyle-CssClass="alternating"
        CommandRowStyle-CssClass="command"
        PagerStyle-CssClass="pager"
        Runat="server" />
    <asp:SqlDataSource
        id="srcProducts"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        SelectCommand="SELECT Title,Director,InStock FROM Products"
        InsertCommand="INSERT Products (Title,Director,InStock)
            VALUES (@Title,@Director,
        Runat="server" />
    </div>
    </form>
</body>
</html>
File: Web.config
<configuration>
  <connectionStrings>
    <add name="Products" 
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>


Inserting a record with the DetailsView control.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <script type="text/javascript">
        function showInsert()
        {
            var divInsert = document.getElementById("divInsert");
            divInsert.style.display = "block";
        }
    </script>
    <title>Show Insert Mode</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="divDisplay">
    <asp:GridView
        id="grdProducts"
        DataSourceID="srcProducts"
        Runat="server" />
    <br />
    <a href="JavaScript:showInsert();">Insert Product</a>
    </div>
    <div id="divInsert">
    <h1>Insert Product</h1>
    <asp:DetailsView
        id="dtlProducts"
        DataSourceID="srcProducts"
        AutoGenerateInsertButton="true"
        AutoGenerateRows="false"
        DefaultMode="Insert"
        Runat="server">
        <Fields>
        <asp:BoundField
            DataField="Title"
            HeaderText="Title:" />
        <asp:BoundField
            DataField="Director"
            HeaderText="Director:" />
        <asp:CheckBoxField
            DataField="InStock"
            HeaderText="In Stock:" />
        </Fields>
    </asp:DetailsView>
    </div>
    <asp:SqlDataSource
        id="srcProducts"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        SelectCommand="SELECT Title,Director,InStock FROM Products"
        InsertCommand="INSERT Products (Title,Director,InStock)
            VALUES (@Title,@Director,
        Runat="server" />
    </form>
</body>
</html>

File: Web.config
<configuration>
  <connectionStrings>
    <add name="Products" 
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>


Link asp:DetaisView with asp:SqlDataSource

<%@ Page Language="VB" AutoEventWireup="false" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         
        <asp:SqlDataSource ID="SqlDataSource1" 
                           runat="server" ConnectionString="YourConnectionString"
                           SelectCommand="SELECT * FROM [Books]"></asp:SqlDataSource>
    
    </div>
        <asp:DetailsView ID="DetailsView1" 
                         runat="server" 
                         AllowPaging="True" 
                         AutoGenerateRows="False"
                         BackColor="LightGoldenrodYellow" 
                         BorderColor="Tan" 
                         BorderWidth="1px" 
                         CellPadding="2"
                         DataKeyNames= "BookID" 
                         DataSourceID="SqlDataSource1" 
                         ForeColor="Black" 
                         GridLines="None"
                         Height="50px" 
                         Width="125px">
            <PagerSettings Mode="NextPrevious" NextPageText="Next &amp;gt;" PreviousPageText="&amp;lt; Previous" />
            <FooterStyle BackColor="Tan" />
            <EditRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
            <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
            <Fields>
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Author" HeaderText="Author" SortExpression="Author" />
                <asp:BoundField DataField="PageCount" HeaderText="Pages" SortExpression="PageCount" />
                <asp:BoundField DataField="YearPublished" HeaderText="Published" SortExpression="YearPublished" />
                <asp:BoundField DataField="Price" DataFormatString="{0:c}" HeaderText="Price" HtmlEncode="False"
                    SortExpression="Price" />
                <asp:BoundField DataField="LastReadOn" DataFormatString="{0:d}" HeaderText="Last Read"
                    HtmlEncode="False" SortExpression="LastReadOn" />
            </Fields>
            <HeaderStyle BackColor="Tan" Font-Bold="True" />
            <AlternatingRowStyle BackColor="PaleGoldenrod" />
        </asp:DetailsView>
        <br />
    </form>
</body>
</html>

File: Web.config
<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyFirstDatabase.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>


Paging Through Data with the DetailsView Control

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Paging</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DetailsView
        id="dtlProducts"
        DataSourceID="srcProducts"
        AllowPaging="true"
        Runat="server" />
    <asp:SqlDataSource
        id="srcProducts"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        SelectCommand="SELECT Id,Title,Director,InStock FROM Products"
        Runat="server" />
    </div>
    </form>
</body>
</html>
File: Web.config
<configuration>
  <connectionStrings>
    <add name="Products" 
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>


Using BoundFields with the DetailsView control.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Show Fields</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DetailsView
        id="dtlProducts"
        DataSourceID="srcProducts"
        AutoGenerateRows="false"
        Runat="server">
        <Fields>
        <asp:BoundField
            DataField="Title"
            HeaderText="Product Title:" />
        <asp:BoundField
            DataField="Director"
            HeaderText="Product Director:" />
        <asp:BoundField
            DataField="Totals"
            DataFormatString="{0:c}"
            HeaderText="Box Office Totals:" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource
        id="srcProducts"
        ConnectionString="<%$ ConnectionStrings:Products %>"
        SelectCommand="SELECT Id,Title,Director,Totals FROM Products
            WHERE Id=1"
        Runat="server" />
    </div>
    </form>
</body>
</html>

File: Web.config
<configuration>
  <connectionStrings>
    <add name="Products" 
         connectionString="Data Source=.\SQLEXPRESS;
         AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
  </connectionStrings>
</configuration>


Using DetailsView

<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="sourceEmployees" 
                           runat="server" 
                           ConnectionString="<%$ ConnectionStrings:Northwind %>"
                           ProviderName="System.Data.SqlClient" 
                           SelectCommand="SELECT * FROM Employees"/>
            <asp:DetailsView ID="DetailsView1" 
                             runat="server" 
                             AutoGenerateRows="False"
                             BorderStyle="Double" 
                             CellPadding="4" 
                             DataKeyNames="EmployeeID" 
                             DataSourceID="sourceEmployees"
                             Font-Names="Verdana" 
                             Font-Size="Small" 
                             ForeColor="#333333" 
                             GridLines="None" 
                             Height="50px"
                             Width="509px" 
                             AutoGenerateDeleteButton="True" 
                             AutoGenerateEditButton="True" 
                             AutoGenerateInsertButton="True" 
                             AllowPaging="True">
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <CommandRowStyle BackColor="#FFFFC0" Font-Bold="True" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <Fields>
                    <asp:BoundField DataField="EmployeeID" 
                                    HeaderText="EmployeeID" 
                                    InsertVisible="False"
                                    ReadOnly="True" 
                                    SortExpression="EmployeeID"/>
                    <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                    <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                    <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                    <asp:BoundField DataField="TitleOfCourtesy" HeaderText="TitleOfCourtesy" SortExpression="TitleOfCourtesy" />
                    <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
                    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                    <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region" />
                    <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
                    <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
                </Fields>
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:DetailsView>
         </div>
    </form>
</body>
</html>
File: Web.config

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.ru/.NetConfiguration/v2.0">
  <appSettings/>
      <connectionStrings>
        <add name="Northwind" connectionString="Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI"/>
      </connectionStrings>
</configuration>