Mega Code Archive

 
Categories / ASP.Net Tutorial / Data Binding
 

Use asp

Each column can be any of several column types The order of your column tags determines the left-to-right order of columns in the GridView. Class                  Description BoundField             text ButtonField            button CheckBoxField          check box                        It¡Â¯s used automatically for true/false fields (in SQL Server, these are fields that use the bit data type). CommandField           provides selection or editing buttons. HyperLinkField         a hyperlink. ImageField             image data from a binary field. TemplateField          specify multiple fields, custom controls, and arbitrary HTML <%@ 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="<%$ ConnectionStrings:ConnectionString %>"             DeleteCommand="DELETE FROM [Books] WHERE [BookID] = @BookID"              InsertCommand="INSERT INTO [Books] ([Title], [Author], [YearPublished], [Price], [LastReadOn], [PageCount]) VALUES (@Title, @Author, @YearPublished, @Price, @LastReadOn, @PageCount)"             SelectCommand="SELECT * FROM [Books]"              UpdateCommand="UPDATE [Books] SET [Title] = @Title, [Author] = @Author, [YearPublished] = @YearPublished, [Price] = @Price, [LastReadOn] = @LastReadOn, [PageCount] = @PageCount WHERE [BookID] = @BookID">             <DeleteParameters>                 <asp:Parameter Name="BookID" Type="Int32" />             </DeleteParameters>             <UpdateParameters>                 <asp:Parameter Name="Title" Type="String" />                 <asp:Parameter Name="Author" Type="String" />                 <asp:Parameter Name="YearPublished" Type="Int32" />                 <asp:Parameter Name="Price" Type="Decimal" />                 <asp:Parameter Name="LastReadOn" Type="DateTime" />                 <asp:Parameter Name="PageCount" Type="Int32" />                 <asp:Parameter Name="BookID" Type="Int32" />             </UpdateParameters>             <InsertParameters>                 <asp:Parameter Name="Title" Type="String" />                 <asp:Parameter Name="Author" Type="String" />                 <asp:Parameter Name="YearPublished" Type="Int32" />                 <asp:Parameter Name="Price" Type="Decimal" />                 <asp:Parameter Name="LastReadOn" Type="DateTime" />                 <asp:Parameter Name="PageCount" Type="Int32" />             </InsertParameters>         </asp:SqlDataSource>          </div>         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="BookID" DataSourceID="SqlDataSource1">             <Columns>                 <asp:CommandField ShowEditButton="True" />                 <asp:BoundField DataField="BookID" HeaderText="Book ID" InsertVisible="False" ReadOnly="True"                     SortExpression="BookID" />                 <asp:TemplateField HeaderText="Title" SortExpression="Title">                     <EditItemTemplate>                         <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Title") %>'></asp:TextBox>                     </EditItemTemplate>                     <ItemTemplate>                         <asp:Label ID="Label1" runat="server" Text='<%# Bind("Title") %>'></asp:Label>                     </ItemTemplate>                 </asp:TemplateField>                 <asp:BoundField DataField="Author" HeaderText="Author" SortExpression="Author" />                 <asp:BoundField DataField="YearPublished" HeaderText="Published" SortExpression="YearPublished" />                 <asp:TemplateField HeaderText="Price" SortExpression="Price">                     <EditItemTemplate>                         $<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Price") %>' Columns="10"></asp:TextBox>                         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox2"                             Display="Dynamic" ErrorMessage="You must enter a price."></asp:RequiredFieldValidator>                         <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox2"                             Display="Dynamic" ErrorMessage="You must enter a valid numeric value greater than or equal to zero."                             Operator="GreaterThanEqual" Type="Double" ValueToCompare="0"></asp:CompareValidator>                     </EditItemTemplate>                     <ItemTemplate>                         <asp:Label ID="Label2" runat="server" Text='<%# Bind("Price", "{0:c}") %>'></asp:Label>                     </ItemTemplate>                 </asp:TemplateField>                 <asp:BoundField DataField="LastReadOn" HeaderText="LastReadOn" SortExpression="Last Read" ApplyFormatInEditMode="True" DataFormatString="{0:d}" HtmlEncode="False" />                 <asp:BoundField DataField="PageCount" HeaderText="PageCount" SortExpression="Pages" />             </Columns>             <RowStyle BackColor="#F7F7DE" />             <SelectedRowStyle Font-Bold="True"/>             <PagerStyle HorizontalAlign="Right" />             <HeaderStyle Font-Bold="True"/>             <AlternatingRowStyle BackColor="White" />             <FooterStyle BackColor="#CCCC99" />             <RowStyle BackColor="#F7F7DE" />             <SelectedRowStyle Font-Bold="True" ForeColor="White" />             <PagerStyle HorizontalAlign="Right" />             <HeaderStyle Font-Bold="True" ForeColor="White" />             <AlternatingRowStyle BackColor="White" />         </asp:GridView>         &nbsp;     </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>