• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Dropdown list as query criteria

Hi, I have one dropdown list, populated with a database table that acts as a query criteria. Everything works fine. However, how can I add a blank value in the dropdown list, ontop of the other values, so it acts as a return all values ? Thanks
0
GPSAddict
Asked:
GPSAddict
  • 4
  • 3
1 Solution
 
GiftsonDJohnCommented:
Hi,

To add a blank value in the top of a dropdown list

DropDownList1.Items.Insert(0, new ListItem(string.Empty, string.Empty))
0
 
GPSAddictAuthor Commented:
GiftsonDJohn: what event should I use ?
0
 
GiftsonDJohnCommented:
On the Page_Load event after you bind the dropdown list, you can use the function.

ex.

DropDownList1.DataSource=DataTable1;
DropDownList1.DataTextField="Column1";
DropDownList1.DataValueField="Column2";
DropDownList1.DataBind();

//Data Binding is Complete. Now we can add blank item into dropdownlist

DropDownList1.Items.Insert(0, new ListItem(string.Empty, string.Empty));
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
GPSAddictAuthor Commented:
GiftsonDJohn: The reason I was asking is I didn't write a single line of code...everything going through the objects...So is there an event that happens just after the binding is complete ? Thanks
0
 
GPSAddictAuthor Commented:
Hello ?
0
 
GiftsonDJohnCommented:
Hi,

Sorry. Got held in my work. All the Data Bound Control will fire DataBound event after every bindings. If you want to code for to return all values if blank value is selected then you have to write query so that it checks for the blank value and return the entire data.

if(DropDownList1.SelectedValue().Equals(string.Empty))
{
    //Query to extract all records
}
else
{
   //Query to extract record based on the dropdown value
}
0
 
GPSAddictAuthor Commented:
Doesn't seem to work, index 0 is already used since the dropdownlist is already populated with my database items ?! Anymore ideas ? Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now