1

I hvae a dropdownlist tied to a SQLDataSource that returns a list of Categories. Since this is used as part of a search function, how do I add a "Select All" category and ensure that such a selection queries all categories?

Thanks much!!

SidC
  • 3,175
  • 14
  • 70
  • 132

2 Answers2

0

Never Use (AppendDataBoundItems="true")!

<asp:DropDownList ID="drpdwnmodel" DataSourceID="Model_Grid" DataTextField="INI" DataValueField="INI" OnSelectedIndexChanged="drprep_SelectedIndexChanged" OnDataBound="DataBoundbyTest_click" AutoPostBack="true" runat="server">
                           <asp:ListItem Value="All" Text="All"></asp:ListItem>
                        </asp:DropDownList>


  <asp:SqlDataSource ID="Model_Grid" runat="server"
                 ConnectionString="<%$ ConnectionStrings:Testconnectionstring%>"
                 SelectCommand="SELECT DISTINCT INI FROM TestTable where [Column] LIKE '%' + @valuename+ '%'  ORDER BY INI ASC">
                 <SelectParameters>
                     <asp:ControlParameter ControlID="drpdwnmodel" Name="valuename"
                         PropertyName="SelectedValue" Type="String" />
                 </SelectParameters>
             </asp:SqlDataSource>

 protected void DataBoundbyTest_click(object sender, EventArgs e)
    {
        drpdwnmodel.Items.Add("All");
        drpdwnmodel.Items.Insert(0, new ListItem("- Select -", "0"));
    }


protected void drprep_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpdwnmodel.SelectedValue.Equals("0"))
        {
            GridView1.DataSourceID = "Test_Grid";
        }
        else {
            if (drpdwnmodel.SelectedValue.Equals("All"))
            {
                GridView1.DataSourceID = "Test_Grid";
            }
            else
            {
                GridView1.DataSourceID = "SqlDataSource1";
            }
        }
        
    }
Mayank Gupta
  • 91
  • 1
  • 5
0

I sometimes use sql for this:

select value, description from reference_table
union
select -1, 'Select all'
Order by 2

There is also the way where you add the item in the markup, then set the AppendDataBoundItems="true"

Also, see this similar question and answer

Community
  • 1
  • 1
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164