DetailsView 2 -- DetailsView with DropdownList

Create an ASP.NET website. Under the website create a folder named "DetailsView". Under the folder create a web form named "DetailsView2".

Database is Northwind. Used table is "Products".

Attribute: AppendDataBoundItems

AppendDataBoundItems는 ASP.NET 2.0 에서 새로 추가된 속성이고 이는 DropDownList의 부모 클래스인 ListControl에 정의된 속성으로, 이 속성이 true로 설정되면 데이터 바인딩 시 현재 존재하는 데이터 요소 뒤에 새로운 데이터 요소가 추가된다.

Result:

==============================================================================================

DetailsView2.aspx

============

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DetailsView2.aspx.cs" Inherits="DetailsView_DetailsView2" %>

<!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>DetailsView 2 Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
            AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="ProductName"
            DataValueField="ProductName">
            <asp:ListItem Text="**** Select product name ***"></asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [ProductName] FROM [Products]"></asp:SqlDataSource>
    </div>
    <br />
    <br />
    <div>
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
        DataSourceID="SqlDataSource2" Height="50px"
            EmptyDataText="There is no data you want." CellPadding="4" ForeColor="#333333"
            GridLines="None">
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
        <RowStyle BackColor="#EFF3FB" />
        <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <Fields>
            <asp:BoundField DataField="ProductName" HeaderText="ProductName"
                SortExpression="ProductName" />
            <asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit"
                SortExpression="QuantityPerUnit" />
            <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
                SortExpression="UnitPrice" />
        </Fields>
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT [ProductName], [QuantityPerUnit], [UnitPrice] FROM [Products] WHERE ([ProductName] = @ProductName)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" DefaultValue="null"
                Name="ProductName" PropertyName="SelectedValue" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

posted @ 2009-01-06 15:22  OOK  阅读(199)  评论(0编辑  收藏  举报