Using a FormView control to display and edit data : FormView « ADO.net Database « ASP.Net






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>

 








Related examples in the same category

1.Basic FormView
2.ItemTemplate in FormView