0

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="<%$ 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" />
1
  • I would like to add a default value in the 2nd dropdownlist that will search ALL of the areas by default Commented Dec 10, 2010 at 23:37

3 Answers 3

1

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();

Sign up to request clarification or add additional context in comments.

1 Comment

Heck, bind both from code-behind. The <asp:xxxDataSource controls are a crazy idea.
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.