开发者

asp.net dropdownlist

I have two dropdownlists for a search, the 1st list is for the city and the second is for the area within the selected city. I would like to add a default value in the 2nd dropdownlist that will search ALL of the areas by default unless a specific area is selected from the list that contains the areaID for a specific search.

 <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="<%$ Connection开发者_如何学运维Strings: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" />


Bind the second from code behind. Create a data table and insert the Select text at the begining and bind the datatable to the DDL. DataTable dt = DataRow dr = dt.NewRow() dr["areaName"] = "All"; dr["SqlArea"] = "All"; tbldata.Rows.InsertAt(dr,0);

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


You can try to add for second dropdownlist OnDataBound="DropArea_DataBound" event.

And use in code:

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

And then when you handle click event, check:

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

Hope it will help with your problem.

Best regards, Dima.


Chris,

I got the idea below from an answer HERE, but basically you want to modify your query to be in the form of the following:

Your Original:

SELECT * FROM [Cities]

Change To:

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

Old question, but you never found an answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜