ASP.NET Tutorial/Data Binding/BoundField

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

Adding the ReadOnly attribute to a BoundField

   <source lang="csharp">

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

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
       <asp:GridView ID="GridView1" Runat="server" DataSourceID="SqlDataSource1"
            DataKeyNames="CustomerID" AutoGenerateColumns="False"
            AllowSorting="True" AllowPaging="True"
            AutoGenerateEditButton="true">
            <PagerStyle HorizontalAlign="Center"></PagerStyle>
            <PagerSettings Position="TopAndBottom" 
                 FirstPageText="Go to the first page" 
                 LastPageText="Go to the last page" Mode="NextPreviousFirstLast">
            </PagerSettings>
           <Columns>
               <asp:BoundField ReadOnly="True" HeaderText="CustomerID" DataField="CustomerID"
                    SortExpression="CustomerID" Visible="False"></asp:BoundField>
               <asp:HyperLinkField HeaderText="CompanyName"
                   DataNavigateUrlFields="CustomerID,Country" SortExpression="CompanyName"
                   DataNavigateUrlFormatString=
                       "http://www.foo.ru/Customer.aspx?id={0}&country={1}"
                   DataTextField="CompanyName">
               </asp:HyperLinkField>
               <asp:BoundField HeaderText="ContactName" DataField="ContactName"
                    SortExpression="ContactName"></asp:BoundField>
               <asp:BoundField HeaderText="ContactTitle" DataField="ContactTitle"
                    SortExpression="ContactTitle"></asp:BoundField>
               <asp:BoundField HeaderText="Address" DataField="Address"
                    SortExpression="Address"></asp:BoundField>
               <asp:BoundField HeaderText="City" DataField="City"
                    SortExpression="City"></asp:BoundField>
               <asp:BoundField HeaderText="Region" NullDisplayText="N/A"
                    DataField="Region" SortExpression="Region"></asp:BoundField>
               <asp:BoundField HeaderText="PostalCode" DataField="PostalCode"
                    SortExpression="PostalCode"></asp:BoundField>
               <asp:BoundField HeaderText="Country" DataField="Country" 
                    SortExpression="Country"></asp:BoundField>
               <asp:BoundField HeaderText="Phone" DataField="Phone" 
                    SortExpression="Phone"></asp:BoundField>
               <asp:BoundField HeaderText="Fax" DataField="Fax" 
                    SortExpression="Fax"></asp:BoundField>
                    
           </Columns>
       </asp:GridView>
       <asp:SqlDataSource ID="SqlDataSource1" Runat="server"
            SelectCommand="SELECT * FROM [Customers]"
            ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>"
            DataSourceMode="DataSet"
            UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName,
                 [ContactName] = @ContactName, [ContactTitle] = @ContactTitle,
                 [Address] = @Address, [City] = @City, [Region] = @Region, 
                 [PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone,
                 [Fax] = @Fax WHERE [CustomerID] = @original_CustomerID">
            <UpdateParameters>
                <asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>
                <asp:Parameter Type="String" Name="ContactName"></asp:Parameter>
                <asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter>
                <asp:Parameter Type="String" Name="Address"></asp:Parameter>
                <asp:Parameter Type="String" Name="City"></asp:Parameter>
                <asp:Parameter Type="String" Name="Region"></asp:Parameter>
                <asp:Parameter Type="String" Name="PostalCode"></asp:Parameter>
                <asp:Parameter Type="String" Name="Country"></asp:Parameter>
                <asp:Parameter Type="String" Name="Phone"></asp:Parameter>
                <asp:Parameter Type="String" Name="Fax"></asp:Parameter>
                <asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>
            </UpdateParameters>
       </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>


Set asp:BoundField for GridView

   <source lang="csharp">

<%@ 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 runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:GridView ID="GridView2" 
                 runat="server" 
                 DataSourceID="sourceProducts"
                 AutoGenerateColumns="False" 
                 DataKeyNames="ProductID">           
       <Columns>
         <asp:BoundField DataField="ProductID" HeaderText="ID" ReadOnly="True" />
         <asp:BoundField DataField="ProductName" HeaderText="Product Name"/>
         <asp:BoundField DataField="UnitPrice" HeaderText="Price" />
         <asp:CommandField ShowEditButton="True" />
       </Columns>
   </asp:GridView>
   <asp:SqlDataSource id="sourceProducts" 
                      runat="server" 
                      SelectCommand="SELECT ProductID, ProductName, UnitPrice FROM Products" 
                      ConnectionString="<%$ ConnectionStrings:Northwind %>"
                      UpdateCommand="UPDATE Products SET ProductName=@ProductName, UnitPrice=CONVERT(money,@UnitPrice) WHERE ProductID=@ProductID">
   </asp:SqlDataSource>
   </form>

</body> </html> File: Web.config <?xml version="1.0"?> <configuration>

 <connectionStrings>
   <add name="Northwind" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI"/>
 </connectionStrings>

</configuration></source>


SqlDataSource with Insert and Update parameters

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" %> <!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:GridView ID="GridView1" 
                         runat="server" 
                         AutoGenerateColumns="False" 
                         DataKeyNames="ProductID"
                         DataSourceID="SqlDataSource1" 
                         EmptyDataText="There are no data records to display.">
               <Columns>
                   <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" />
                   <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                   <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" SortExpression="SupplierID" />
                   <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
                   <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />
                   <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
                   <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
                   <asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder" SortExpression="UnitsOnOrder" />
                   <asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel" SortExpression="ReorderLevel" />
                   <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued" />
               </Columns>
           </asp:GridView>
           <asp:SqlDataSource ID="SqlDataSource1" 
                              runat="server" 
                              ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
               DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" InsertCommand="INSERT INTO [Products] ([ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued]) VALUES (@ProductName, @SupplierID, @CategoryID, @QuantityPerUnit, @UnitPrice, @UnitsInStock, @UnitsOnOrder, @ReorderLevel, @Discontinued)"
               ProviderName="<%$ ConnectionStrings:NorthwindConnectionString.ProviderName %>"
               SelectCommand="SELECT [ProductID], [ProductName], [SupplierID], [CategoryID], [QuantityPerUnit], [UnitPrice], [UnitsInStock], [UnitsOnOrder], [ReorderLevel], [Discontinued] FROM [Products]"
               UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [SupplierID] = @SupplierID, [CategoryID] = @CategoryID, [QuantityPerUnit] = @QuantityPerUnit, [UnitPrice] = @UnitPrice, [UnitsInStock] = @UnitsInStock, [UnitsOnOrder] = @UnitsOnOrder, [ReorderLevel] = @ReorderLevel, [Discontinued] = @Discontinued WHERE [ProductID] = @ProductID">
               <InsertParameters>
                   <asp:Parameter Name="ProductName" Type="String" />
                   <asp:Parameter Name="SupplierID" Type="Int32" />
                   <asp:Parameter Name="CategoryID" Type="Int32" />
                   <asp:Parameter Name="QuantityPerUnit" Type="String" />
                   <asp:Parameter Name="UnitPrice" Type="Decimal" />
                   <asp:Parameter Name="UnitsInStock" Type="Int16" />
                   <asp:Parameter Name="UnitsOnOrder" Type="Int16" />
                   <asp:Parameter Name="ReorderLevel" Type="Int16" />
                   <asp:Parameter Name="Discontinued" Type="Boolean" />
               </InsertParameters>
               <UpdateParameters>
                   <asp:Parameter Name="ProductName" Type="String" />
                   <asp:Parameter Name="SupplierID" Type="Int32" />
                   <asp:Parameter Name="CategoryID" Type="Int32" />
                   <asp:Parameter Name="QuantityPerUnit" Type="String" />
                   <asp:Parameter Name="UnitPrice" Type="Decimal" />
                   <asp:Parameter Name="UnitsInStock" Type="Int16" />
                   <asp:Parameter Name="UnitsOnOrder" Type="Int16" />
                   <asp:Parameter Name="ReorderLevel" Type="Int16" />
                   <asp:Parameter Name="Discontinued" Type="Boolean" />
                   <asp:Parameter Name="ProductID" Type="Int32" />
               </UpdateParameters>
               <DeleteParameters>
                   <asp:Parameter Name="ProductID" Type="Int32" />
               </DeleteParameters>
           </asp:SqlDataSource>
   
   </form>

</body> </html> File: Web.config <?xml version="1.0"?> <configuration>

 <appSettings/>
 <connectionStrings>
 <add name="NorthwindConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"
  providerName="System.Data.SqlClient" />
</connectionStrings>
 <system.web>
   <compilation debug="true"/>
   <authentication mode="Windows"/>
 </system.web>

</configuration></source>


Using BoundFields with the GridView control

   <source lang="csharp">

The most important three properties of the BoundField class are the DataField, DataFormatString, and HeaderText properties. <%@ 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 BoundField</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:GridView
       id="grdProducts"
       DataSourceID="srcProducts"
       AutoGenerateColumns="false"
       Runat="server">
       <Columns>
       <asp:BoundField
           DataField="Title"
           HeaderText="Product Title" />
       <asp:BoundField
           DataField="Director"
           HeaderText="Product Director" />
       <asp:BoundField
           DataField="Totals"
           DataFormatString="{0:c}"
           HtmlEncode="false"
           HeaderText="Box Office Totals" />
       </Columns>
   </asp:GridView>
   <asp:SqlDataSource
       id="srcProducts"
       ConnectionString="<%$ ConnectionStrings:Products %>"
       SelectCommand="SELECT * FROM Products"
       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></source>