2010-12-10 72 views
0

我有兩個搜索下拉列表,第一個列表是針對城市,第二個列表是針對所選城市的區域。我希望在第二個下拉列表中添加一個默認值,默認情況下會搜索所有區域,除非從包含特定搜索的區域ID的列表中選擇特定區域。asp.net dropdownlist

<asp:DropDownList ID="DropDownList1" runat="server" 
DataSourceID="SqlDataSource1" DataTextField="cityName" AutoPostBack="true" DataValueField="cityID"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
SelectCommand="SELECT * FROM [Cities]"></asp:SqlDataSource> 
<asp:DropDownList ID="DropArea" runat="server" DataSourceID="SqlArea" 
    DataTextField="areaName" DataValueField="areaID"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="SqlArea" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [area] WHERE ([cityID] = @cityID)"> 
    <SelectParameters> 
     <asp:ControlParameter ControlID="DropDownList1" DefaultValue="0" Name="cityID" 
      PropertyName="SelectedValue" Type="Int16" /> 
    </SelectParameters> 
</asp:SqlDataSource> 
    <asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" /> 
+0

這裏有問題嗎? – James 2010-12-10 23:34:46

+0

我想在第二個下拉列表中添加一個默認值,該默認值將默認搜索所有區域 – Chris 2010-12-10 23:37:38

回答

1

把後面的代碼綁在第二位。創建數據表並在開始時插入Select文本並將數據表綁定到DDL。 DataTable dt = DataRow dr = dt.NewRow() dr [「areaName」] =「All」; dr [「SqlArea」] =「全部」; tbldata.Rows.InsertAt(dr,0);

DropArea.DataSource = dt; DropArea.DataBind();

+0

Heck,從代碼隱藏中綁定兩者。 joelt 2010-12-11 02:43:44

0

您可以嘗試爲第二個下拉列表添加OnDataBound =「DropArea_DataBound」事件。在代碼

及用途:

protected void DropArea_DataBound(object sender, EventArgs e) 
{ 
    DropArea.Items.Insert(0, new ListItem() {Value = "-1", Text = "ALL"}); 
} 

然後當你處理單擊事件,檢查:

var value = DropArea.SelectedItem.Value; 
if(string.equals(value, '-1') 
{ 
    use your logic here 
} 

希望它會與你的問題有所幫助。

最好的問候,迪馬。

+0

我得到-1值的錯誤 – Chris 2010-12-11 02:03:26

0

克里斯,

我接到了一個答案HERE下面的想法,但基本上要修改您的查詢將在下面的形式:

您的原創:

SELECT * FROM [Cities] 

更改爲:

SELECT City FROM [Cities] 
UNION ALL 
SELECT 'ALL' 

老問題,但你永遠不會fo和一個答案。