ASP.Net/ADO.net Database/FormView — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:51, 26 мая 2010
Basic FormView
<%@ 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") %><br />
<%# Eval("City") %>, <%# Eval("Country") %>,
<%# Eval("PostalCode") %><br />
<%# 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>
ItemTemplate in FormView
<%@ 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">
<div id="container">
Book Details
<asp:FormView ID="frmvBooks" runat="server" DataSourceID="dsBooks"
AllowPaging="true" DataKeyNames="Isbn">
<ItemTemplate>
ISBN: <br />
<asp:Label ID="labIsbn" runat="server"
Text="<%# Eval("ISBN") %>" /><br />
Title:<br />
<asp:Label ID="labTitle" runat="server"
Text="<%# Eval("Title") %>" /><br />
Publisher:<br />
<asp:Label ID="labPublisher" runat="server"
Text="<%# Eval("PublisherName") %>" /><br />
Year:<br />
<asp:Label ID="labYear" runat="server"
Text="<%# Eval("YearPublished") %>" /><br />
Description:<br />
<asp:Label ID="txtDecs" runat="server"
Text="<%# Eval("BriefDescription") %>" /><br/>
</div>
</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>
Using a FormView control to display and edit data
<!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">
<div>
<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><br />
CompanyName:
<asp:TextBox Text="<%# Bind("CompanyName") %>" Runat="server"
ID="CompanyNameTextBox"></asp:TextBox><br />
ContactName:
<asp:TextBox Text="<%# Bind("ContactName") %>" Runat="server"
ID="ContactNameTextBox"></asp:TextBox><br />
ContactTitle:
<asp:TextBox Text="<%# Bind("ContactTitle") %>" Runat="server"
ID="ContactTitleTextBox"></asp:TextBox><br />
Address:
<asp:TextBox Text="<%# Bind("Address") %>" Runat="server"
ID="AddressTextBox"></asp:TextBox><br />
City:
<asp:TextBox Text="<%# Bind("City") %>" Runat="server"
ID="CityTextBox"></asp:TextBox><br />
Region:
<asp:TextBox Text="<%# Bind("Region") %>" Runat="server"
ID="RegionTextBox"></asp:TextBox><br />
PostalCode:
<asp:TextBox Text="<%# Bind("PostalCode") %>" Runat="server"
ID="PostalCodeTextBox"></asp:TextBox><br />
Country:
<asp:TextBox Text="<%# Bind("Country") %>" Runat="server"
ID="CountryTextBox"></asp:TextBox><br />
Phone:
<asp:TextBox Text="<%# Bind("Phone") %>" Runat="server"
ID="PhoneTextBox"></asp:TextBox><br />
Fax:
<asp:TextBox Text="<%# Bind("Fax") %>" Runat="server"
ID="FaxTextBox"></asp:TextBox><br />
<br />
<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><br />
ContactTitle:<asp:Label ID="ContactTitleLabel" Runat="server"
Text="<%# Bind("ContactTitle") %>">
</asp:Label><br />
<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><br />
Fax:<asp:Label ID="FaxLabel" Runat="server"
Text="<%# Bind("Fax") %>">
</asp:Label><br />
<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>
</div>
</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>