如果你經常寫報表應該會常使用到下拉選單,目的是讓使用者設定查詢資料的條件,這對使用者操作介面上是非常方便的,但是對於程式開發者而言是非常痛苦的一件事,因為對資料的查詢條件越多,就必須結合更多的條件指令,若查詢條件中多了一個ALL的選項讓使用者可以選擇瀏覽全部資料時,一般都會在程式中加上很多的判斷式來確認使用者是否選擇了ALL選項,然後以組SQL 字串的方式處理之,但萬一下拉選單有好多個時,你的判斷式便會複雜到自己都無法處理。


大部分的程式開發者都會使用下列的方式來解決此問題:

如果有三個以上的DropDownList,那程式邏輯會顯得非常的複雜,因此,花了不少時間想到了一個自己認為很不錯的方法,既省時又可避免因為程式邏輯撰寫錯誤而造成資料顯示不正確的問題。那就是應用SQL的 ISNULL() 函式來處理,並搭配 X = X的等式來處理ALL的問題,接下來就來實作這個解決方法吧!
首先我們先拉出畫面如下:

產品主類別: DropDownList1 -> SqlDataSource1
產品次類別: DropDownList2 -> SqlDataSource2 (ControlParameter DropDownList1)
產品次次類別: DropDownList3 -> SqlDataSource3(ControlParameter DropDownList1、 DropDownList2)
GridView -> SqlDataSource4
.ASPX (請注意黃色標住的地方是關鍵)

網頁原始碼
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownlist.aspx.cs" Inherits="DropDownlist" %>
<!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>產品列表</title>
</head>
<body>
<form id="form1" runat="server">
<div>
產品主類別:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" AppendDataBoundItems="true"
DataSourceID="SqlDataSource1" DataTextField="Product_Class" DataValueField="Product_Class">
<asp:ListItem Text="ALL" Value=""></asp:ListItem>
</asp:DropDownList>
產品次類別:<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2"
DataTextField="Product_Type" DataValueField="Product_Type" OnDataBound="DropDownList2_DataBound">
</asp:DropDownList>
產品次次類別:<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3"
DataTextField="Product_SubType" DataValueField="Product_SubType" OnDataBound="DropDownList3_DataBound">
</asp:DropDownList>
<br />
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#999999"
BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" AutoGenerateColumns="true"
DataSourceID="SqlDataSource4">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#DCDCDC" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:cht %>"
SelectCommand="SELECT DISTINCT Product_Class FROM Product_Class ORDER BY Product_Class">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:cht %>"
SelectCommand="SELECT DISTINCT [Product_Type] FROM [Product_Class] WHERE (Product_Type<>'') AND (Product_Class=@Product_Class) ORDER BY [Product_Type]">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Product_Class" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:cht %>"
SelectCommand="SELECT DISTINCT [Product_SubType] FROM [Product_Class] WHERE ([Product_SubType]<>'') AND (Product_Class=@Product_Class) AND (Product_Type = @Product_Type) ORDER BY Product_SubType">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Product_Class" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList2" Name="Product_Type" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:cht %>"
CancelSelectOnNullParameter="False" SelectCommand="SELECT sid as 編號,product_class as 產品主類別 ,
product_MainClass as 產品次類別,product_SubClass as 產品次次類別,
product_type as 產品名稱 FROM [Product]
WHERE product_class=ISNULL(@product_class,product_class)
AND product_MainClass=ISNULL(@product_MainClass,product_MainClass)
AND product_SubClass=ISNULL(@product_SubClass,product_SubClass)
ORDER BY [sid]">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="product_class" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList2" Name="product_MainClass" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DropDownList3" Name="product_SubClass" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
.CS
原始碼下載: DropDownlist _Code.rar
2008/7/19 13:14|
閱讀數 : 921
|
我要推薦
|
|
文章分類:
ASP.NET 2.0
訂閱
DotBlogs Tags:
ASP.NET
DropDownlist
GridView