ASP.Net/ADO.net Database/FormView

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

Basic FormView

   <source lang="csharp">

<%@ 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">
       <asp:SqlDataSource ID="sourceEmployees" 
                          runat="server" 
                          ConnectionString="<%$ ConnectionStrings:Northwind %>"
                          ProviderName="System.Data.SqlClient" 
                          SelectCommand="SELECT * FROM Employees"/>
       <asp:FormView ID="FormView1" runat="server" DataSourceID="sourceEmployees">
                 <ItemTemplate>
                   <%# Eval("EmployeeID") %> - 
                   <%# Eval("TitleOfCourtesy") %> <%# Eval("FirstName") %>
                   <%# Eval("LastName") %>
                   <%# Eval("Address") %>
<%# Eval("City") %>, <%# Eval("Country") %>, <%# Eval("PostalCode") %>
<%# Eval("HomePhone") %> <%# Eval("Notes") %> </ItemTemplate> </asp:FormView> </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>

</source>
   
  


ItemTemplate in FormView

   <source lang="csharp">

<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server">

 <title>Edit FormView</title>

</head> <body>

 <form id="form1" runat="server">
     Book Details
         <asp:FormView ID="frmvBooks" runat="server" DataSourceID="dsBooks" 
              AllowPaging="true" DataKeyNames="Isbn">
          <ItemTemplate>
            ISBN: 
<asp:Label ID="labIsbn" runat="server" Text="<%# Eval("ISBN") %>" />
Title:
<asp:Label ID="labTitle" runat="server" Text="<%# Eval("Title") %>" />
Publisher:
<asp:Label ID="labPublisher" runat="server" Text="<%# Eval("PublisherName") %>" />
Year:
<asp:Label ID="labYear" runat="server" Text="<%# Eval("YearPublished") %>" />
Description:
<asp:Label ID="txtDecs" runat="server" Text="<%# Eval("BriefDescription") %>" />
          </ItemTemplate>
           <EmptyDataTemplate>
              No book matched this ISBN
           </EmptyDataTemplate>
          
           
           <PagerStyle CssClass="singleBookTitle" />
           <PagerSettings Mode="NextPreviousFirstLast" 
              FirstPageImageUrl="images/page_first.gif"
              LastPageImageUrl="images/page_last.gif"
              NextPageImageUrl="images/page_next.gif"
              PreviousPageImageUrl="images/page_previous.gif"/>
        </asp:FormView>
   </div>
     <asp:SqlDataSource ID="dsBooks" runat="server"
        ProviderName="System.Data.OleDb" 
        ConnectionString="<%$ ConnectionStrings:Books %>"
        SelectCommand="SELECT ISBN,Title,PublisherName,Books.PublisherId,YearPublished,BriefDescription FROM Books INNER JOIN Publishers ON Books.PublisherId=Publishers.PublisherId WHERE ISBN=@isbn">
        <SelectParameters>
           <asp:QueryStringParameter Name="isbn" QueryStringField="id" />
        </SelectParameters>       
        
     </asp:SqlDataSource>
                  
 </form>

</body> </html>

</source>
   
  


Using a FormView control to display and edit data

   <source lang="csharp">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:FormView ID="FormView1" Runat="server" DataSourceID="SqlDataSource1"
            DataKeyNames="CustomerID" AllowPaging="True">
           <EditItemTemplate>
               CustomerID:
               <asp:Label Text="<%# Eval("CustomerID") %>" Runat="server"
                    ID="CustomerIDLabel1">
               </asp:Label>
CompanyName: <asp:TextBox Text="<%# Bind("CompanyName") %>" Runat="server" ID="CompanyNameTextBox"></asp:TextBox>
ContactName: <asp:TextBox Text="<%# Bind("ContactName") %>" Runat="server" ID="ContactNameTextBox"></asp:TextBox>
ContactTitle: <asp:TextBox Text="<%# Bind("ContactTitle") %>" Runat="server" ID="ContactTitleTextBox"></asp:TextBox>
Address: <asp:TextBox Text="<%# Bind("Address") %>" Runat="server" ID="AddressTextBox"></asp:TextBox>
City: <asp:TextBox Text="<%# Bind("City") %>" Runat="server" ID="CityTextBox"></asp:TextBox>
Region: <asp:TextBox Text="<%# Bind("Region") %>" Runat="server" ID="RegionTextBox"></asp:TextBox>
PostalCode: <asp:TextBox Text="<%# Bind("PostalCode") %>" Runat="server" ID="PostalCodeTextBox"></asp:TextBox>
Country: <asp:TextBox Text="<%# Bind("Country") %>" Runat="server" ID="CountryTextBox"></asp:TextBox>
Phone: <asp:TextBox Text="<%# Bind("Phone") %>" Runat="server" ID="PhoneTextBox"></asp:TextBox>
Fax: <asp:TextBox Text="<%# Bind("Fax") %>" Runat="server" ID="FaxTextBox"></asp:TextBox>

<asp:Button ID="Button2" Runat="server" Text="Update" CommandName="update" /> <asp:Button ID="Button3" Runat="server" Text="Cancel" CommandName="cancel" /> </EditItemTemplate> <ItemTemplate> CustomerID:<asp:Label ID="CustomerIDLabel" Runat="server" Text="<%# Bind("CustomerID") %>"> </asp:Label> CompanyName:<asp:Label ID="CompanyNameLabel" Runat="server" Text="<%# Bind("CompanyName") %>"> </asp:Label> ContactName:<asp:Label ID="ContactNameLabel" Runat="server" Text="<%# Bind("ContactName") %>"> </asp:Label>
ContactTitle:<asp:Label ID="ContactTitleLabel" Runat="server" Text="<%# Bind("ContactTitle") %>"> </asp:Label>
<asp:Label ID="AddressLabel" Runat="server" Text="<%# Bind("Address") %>"> </asp:Label></td> <asp:Label ID="CityLabel" Runat="server" Text="<%# Bind("City") %>"> </asp:Label></td> <asp:Label ID="RegionLabel" Runat="server" Text="<%# Bind("Region") %>"> </asp:Label></td> <asp:Label ID="PostalCodeLabel" Runat="server" Text="<%# Bind("PostalCode") %>"> </asp:Label> <asp:Label ID="CountryLabel" Runat="server" Text="<%# Bind("Country") %>"> </asp:Label></td> Phone:<asp:Label ID="PhoneLabel" Runat="server" Text="<%# Bind("Phone") %>"> </asp:Label>
Fax:<asp:Label ID="FaxLabel" Runat="server" Text="<%# Bind("Fax") %>"> </asp:Label>
<asp:Button ID="Button1" Runat="server" Text="Edit" CommandName="edit" /> </ItemTemplate> </asp:FormView> <asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT * FROM [Customers]" ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>"> </asp:SqlDataSource>
   </form>

</body> </html> File: Web.config <configuration>

 <appSettings/>
 <connectionStrings>
       <add name="AppConnectionString1" 
            connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
            providerName="System.Data.SqlClient" />
   </connectionStrings>
 <system.web>
   <compilation debug="true" strict="false" explicit="true">
     <codeSubDirectories>
       <add directoryName="VB"></add>
       <add directoryName="CS"></add>
     </codeSubDirectories>
   </compilation>
   <pages>
     <namespaces>
       <clear/>
       <add namespace="System"/>
       <add namespace="System.Collections"/>
       <add namespace="System.Collections.Specialized"/>
       <add namespace="System.Configuration"/>
       <add namespace="System.Text"/>
       <add namespace="System.Text.RegularExpressions"/>
       <add namespace="System.Web"/>
       <add namespace="System.Web.Caching"/>
       <add namespace="System.Web.SessionState"/>
       <add namespace="System.Web.Security"/>
       <add namespace="System.Web.Profile"/>
       <add namespace="System.Web.UI"/>
       <add namespace="System.Web.UI.WebControls"/>
       <add namespace="System.Web.UI.WebControls.WebParts"/>
       <add namespace="System.Web.UI.HtmlControls"/>
     </namespaces>
   </pages>
   <authentication mode="Windows"></authentication>
   <identity impersonate="true"/>
 </system.web>

</configuration>

</source>