dropdownlist prob in ASP>NET

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 26
Reputation: s1986 is an unknown quantity at this point 
Solved Threads: 1
s1986 s1986 is offline Offline
Light Poster

dropdownlist prob in ASP>NET

 
0
  #1
Feb 20th, 2008
m new 2 asp.net.
my prob is after populating the dropdownlist with data from the database wen i select an item it select only the first item.following is my code.where did i go wrong

code:-

con3.start_connection()
dr = con3.executequery("SELECT city_name from City")
DropDownList1.Items.Clear()
While dr.Read
DropDownList1.Items.Add(dr(0))
End While
dr.Close()
con3.close_connection()


dr is the datareader and con3 is the object of the class that makes the connection and runs the query
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: dropdownlist prob in ASP>NET

 
0
  #2
Feb 20th, 2008
put the code in this block :

if(!IsPostBack)
{
// your code goes here
}

Explanation : If you bind data to your dropdownlist, it wont remember its state(example, last selected item before postback)
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: srinunarra is an unknown quantity at this point 
Solved Threads: 0
srinunarra srinunarra is offline Offline
Newbie Poster

Re: dropdownlist prob in ASP>NET

 
0
  #3
Feb 20th, 2008
Hi,
Set AUTOPOSTBACK propery of DROPDOWNLIST to TRUE.
Regards,
SrinivasaRao.Narra,
Mumbai.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: dropdownlist prob in ASP>NET

 
0
  #4
Feb 20th, 2008
It's because of your IsPostBack method is not active.

You need to add "If Not Page.IsPostBack Then" to the beginning of your code, and "End If" to the end.

The reason for this is that when you click on it and it posts back to the server, the server goes through your entire page and fires off the code again. It will, however, skip over the code that is within the Not Page.IsPostBack.

If you do not have IsPostBack method, then it will always repopulate the dropdownlist. Thus, making it selected index zero, which is the first item in the dropdownlist.

Use IsPostBack to your advantage. You can use it to only execute code when information is sent back to the server on the same page.

Examples:
  1. If Not Page.IsPostBack Then
  2. response.write("Hi, page loaded for the first time.")
  3. Else
  4. response.write("The page was sent back to the server, then received again. This is a PostBack.")
  5. End If
Now when a user visits this page, they will see "Hi, page loaded for the first time.", but if you send the page back to the server (like most asp.net server controls do), the page will be rewritten as "The page was sent back to the server, then received again. This is a PostBack.". These are post backs. Play with them, they do wonders!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 26
Reputation: s1986 is an unknown quantity at this point 
Solved Threads: 1
s1986 s1986 is offline Offline
Light Poster

Re: dropdownlist prob in ASP>NET

 
0
  #5
Feb 22nd, 2008
thanks a lot guys.your suggestions really worked
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC