Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
asp.net下拉列表_Asp.net - Fatal编程技术网

asp.net下拉列表

asp.net下拉列表,asp.net,Asp.net,我有两个下拉列表用于搜索,第一个列表用于城市,第二个列表用于所选城市内的区域。我想在第二个下拉列表中添加一个默认值,默认情况下将搜索所有区域,除非从包含特定搜索区域ID的列表中选择了特定区域 <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="cityName" AutoPostBack="true" DataValueField="cityID

我有两个下拉列表用于搜索,第一个列表用于城市,第二个列表用于所选城市内的区域。我想在第二个下拉列表中添加一个默认值,默认情况下将搜索所有区域,除非从包含特定搜索区域ID的列表中选择了特定区域

 <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" />

从代码后面绑定第二个。创建一个数据表,在开头插入Select文本,并将数据表绑定到DDL。 数据表dt= DataRow dr=dt.NewRow() dr[“areaName”]=“全部”; dr[“SqlArea”]=“全部”; tbldata.Rows.InsertAt(dr,0)

DropArea.DataSource=dt;
DropArea.DataBind()

您可以尝试添加第二个dropdownlist OnDataBound=“DropArea\u 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
}
希望它能帮助解决你的问题

向你问好,迪玛。

克里斯

我从一个答案中得到了以下想法,但基本上您希望将查询修改为以下形式:

您的原件:

SELECT * FROM [Cities]
改为:

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

旧问题,但您从未找到答案。

我想在第二个下拉列表中添加一个默认值,该值将按默认值搜索所有区域。检查,从代码隐藏绑定这两个区域。这个