Trouble retrieving value from drop down list

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 38
Reputation: thacravedawg is an unknown quantity at this point 
Solved Threads: 0
thacravedawg thacravedawg is offline Offline
Light Poster

Trouble retrieving value from drop down list

 
0
  #1
May 4th, 2009
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.

  1. protected void btnTypeSearch_Click(object sender, EventArgs e)
  2. {
  3. photoPanel.Controls.Clear();
  4.  
  5. string type = ddType.SelectedItem.Text;
  6.  
  7. DataLayer typeLayer = new DataLayer();
  8. DataTable dt = typeLayer.fillTbl("SELECT * FROM Trees WHERE Type='" + type + "'");
  9.  
  10. fillGallery(dt);
  11. }

In reality I simply need a way to store the value of the currently selected item in a variable for future use.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Trouble retrieving value from drop down list

 
0
  #2
May 4th, 2009
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:
  1.  
  2. if(IsPostBack==false) {
  3. ..
  4. .. Place your databind code here..
  5. }
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC