| | |
dropdownlist prob in ASP>NET
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 26
Reputation:
Solved Threads: 1
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
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
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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:
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!
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)
If Not Page.IsPostBack Then response.write("Hi, page loaded for the first time.") Else response.write("The page was sent back to the server, then received again. This is a PostBack.") End If
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: Recordset and VB.NET
- Next Thread: Printing from as ASP.Net web page
| Thread Tools | Search this Thread |
.net 2.0 activexcontrol advice ajax alltypeofvideos application asp asp.net bc30451 bottomasp.net box browser button c# c#gridviewcolumn checkbox click commonfunctions confirmationcodegeneration css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dynamically edit expose feedback fileuploader fill flash form formatdecimal forms formview grid gridview gudi homeedition hosting iframe iis javascript jquery listbox login microsoft mono mouse mssql multistepregistration news numerical objects opera panelmasterpagebuttoncontrols parent radio redirect registration relationaldatabases reportemail rotatepage save schoolproject search security select silverlight smartcard smoobjects software sql-server sqlserver2005 suse textbox tracking unauthorized validation vb.net video videos view virtualdirectory vista visualstudio web webapplications webdevelopemnt webprogramming webservice xml xsl youareanotmemberofthedebuggerusers






