Navigation

Search

Categories

On this page

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 112
This Year: 50
This Month: 0
This Week: 0
Comments: 0

Sign In

 Monday, December 10, 2007
Monday, December 10, 2007 9:42:41 PM (Eastern Standard Time, UTC-05:00) ( )

This is a more advanced example of updating and editing a Gridview control

<asp:sqldatasource ID="ds1" runat="server"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectString %>"
    SelectCommand="SELECT ProductID, ProductName, QuantityPerUnit, Discontinued, UnitPrice FROM Products"
    UpdateCommand="UPDATE Products SET QuantityPerUnit=@QuantityPerUnit, Discontinued=@Discontinued, UnitPrice=@UnitPrice WHERE ProductID=@ProductID"
    DeleteCommand="DELETE FROM Products WHERE ProductID=@ProductID"
   />

  <asp:GridView ID="grid1" runat="server" DataSourceID="ds1"
    DataKeyNames="ProductID"
    AutoGenerateColumns="False"
    AllowSorting="True"
    AllowPaging="True" 
    PageSize="5">
    <Columns>

      <asp:ButtonField ButtonType="Button" DataTextField="ProductID" SortExpression="ProductID"
        HeaderText="ID" />

      <asp:HyperLinkField DataTextField="ProductName"
        DataNavigateUrlFields="ProductID,ProductName"
        DataNavigateUrlFormatString="http://www.site-that-shows-more-info.com/products?product={0}&amp;name={1}"
        SortExpression="ProductName" HeaderText="Product"
        ItemStyle-Font-Bold="True" ItemStyle-BackColor="Yellow" />

      <asp:BoundField DataField="QuantityPerUnit" HeaderText="Packaging"  />

      <asp:CheckBoxField DataField="Discontinued" HeaderText="N/A" />

      <asp:TemplateField HeaderText="Price"  SortExpression="UnitPrice"
        ItemStyle-Font-Bold="True">
        <ItemTemplate>
          <asp:Label runat="server" text='<%# Eval("UnitPrice", "${0:F2}") %>' />
        </ItemTemplate>
        <AlternatingItemTemplate>
          <asp:Label runat="server" ForeColor="DarkGray" text='<%# Eval("UnitPrice", "${0:F2}") %>' />
        </AlternatingItemTemplate>
        <EditItemTemplate>
          <asp:TextBox ID="UnitPrice" runat="server" Text='<%#Bind("UnitPrice")%>' />
          <asp:RequiredFieldValidator ID="rfv1" runat="server"
          ControlToValidate="UnitPrice" Text="You must enter the price" />
        </EditItemTemplate>
      </asp:TemplateField>
      
      <asp:CommandField ButtonType="Image" ShowCancelButton="True"
        ShowEditButton="True" ShowDeleteButton="True"
        CancelImageUrl="s.gif" EditImageUrl="q.gif"
        UpdateImageUrl="i.gif" DeleteImageUrl="x.gif"
        CancelText="Cancel this update" EditText="Edit this row"
        UpdateText="Apply these changes" DeleteText="Delete this row" />

    </Columns>
    <HeaderStyle Font-Bold="true" Font-Names="Verdana" />
    <RowStyle Font-Names="Verdana" />

  </asp:GridView>
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):