943,908 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 960
  • ASP.NET RSS
Feb 20th, 2008
0

dropdownlist prob in ASP>NET

Expand Post »
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
Reputation Points: 10
Solved Threads: 1
Light Poster
s1986 is offline Offline
26 posts
since Feb 2008
Feb 20th, 2008
0

Re: dropdownlist prob in ASP>NET

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)
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Feb 20th, 2008
0

Re: dropdownlist prob in ASP>NET

Hi,
Set AUTOPOSTBACK propery of DROPDOWNLIST to TRUE.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
srinunarra is offline Offline
4 posts
since Feb 2008
Feb 20th, 2008
0

Re: dropdownlist prob in ASP>NET

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:
ASP.NET Syntax (Toggle Plain Text)
  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!
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007
Feb 22nd, 2008
0

Re: dropdownlist prob in ASP>NET

thanks a lot guys.your suggestions really worked
Reputation Points: 10
Solved Threads: 1
Light Poster
s1986 is offline Offline
26 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Recordset and VB.NET
Next Thread in ASP.NET Forum Timeline: Printing from as ASP.Net web page





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC