I have a drop down list on my my aspx page. This box is populated from my sqlserver db. I want to allow people to search based on the value they select from the drop down list. The problem I am encountering is that it always returns the value of the first item in my list box. I am guessing my problem spurs from my lack of knowledge of postback, please help me.

This is the code im trying to use to retrieve the value of the ddl and use it as a parameter for another method.

protected void btnTypeSearch_Click(object sender, EventArgs e)
        {
                photoPanel.Controls.Clear();

                string type = ddType.SelectedItem.Text;

                DataLayer typeLayer = new DataLayer();
                DataTable dt = typeLayer.fillTbl("SELECT * FROM Trees WHERE Type='" + type + "'");

                fillGallery(dt);
        }

In reality I simply need a way to store the value of the currently selected item in a variable for future use.

Dear,

If you are loading DropDownList in Page_Load event then the code of databinding must be execute once.

IsPostBack property of Page class is used to determine whether a page is loaded first time or not.

You may add your databind code like this:

if(IsPostBack==false) {
       .. 
       ..  Place your databind code here..
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.