|
-
Oct 20th, 2005, 10:51 AM
#1
Thread Starter
New Member
Web form selected reset
Hi guys. I'm just getting started with VB and I'm trying to make a web form with a database backend. I have a couple dropdowns in the form that are populated on PageLoad with db info, and I'd like to have it so when you pick a selection from the dropdown, other text elements in the form change. In a Windows Form this is trivial and I've mastered the concept already, but in a Web Form, there'd have to be a bit of back and forth with the server to handle this event.
I managed to get it halfway working by changing something to "PostBack" or something like that (it was two weeks ago and now I can't even find the property... if someone can point me back to this setting I'd feel less lost =)) and now the form properly fires the "Selection_Changed" method for the dropdown in question.
The problem is that when the event is fired, it reloads the page and the PageLoad method is called again and the dropdown is reset, i.e. "getSelectedItem" always returns the default dropdown.
Example:
1. Load the page, the dropdown pulls from the db and contains 4 items: "Please choose a client...", "Microsoft Inc.", "IBM" and "Dell".
2. You choose "Microsoft Inc." from the dropdown, and the page immediately refreshes (which is good), but the dropdown is reset and repopulated and the form populates the text field with the values as if "Please select a client..." was selected, which it now is.
Originally, I didn't bind the dropdown to a dataset or anything, I just added items to it on a loop from a DataReader on PageLoad. I'm in the process of creating some DataSets and binding them to the dropdowns, but I imagine the PageLoad issue will still be there after I do that.
Am I doing this all wrong? Where should I put the "db query/populate dropdown" calls to only have them called the first time (i.e. on a GET request only). And is there any trick to the "update form info on dropdown selection change" thing that I may not get?
Regards,
Jon Heese
Last edited by jonheese; Oct 20th, 2005 at 11:17 AM.
-
Oct 20th, 2005, 10:52 AM
#2
Thread Starter
New Member
Re: Web form selected reset
Also I figured this was more of a theory question, but I can post snippets of my code if you want. Thanks.
Regards,
Jon Heese
-
Oct 20th, 2005, 11:08 AM
#3
Thread Starter
New Member
Re: Web form selected reset
Aha, I re-found the "AutoPostBack" property in the DropDown control.
Okay, so I set "AutoPostBack" to True and now a change in that dropdown selection POSTs the form, I suppose?
This is starting to make more sense now... It's amazing what just writing out a problem will do for making it clearer.
Okay, so going off the top of my head:
First of all, I've not yet put a Submit button on the form yet, so I haven't even started dealing with how form submissions will be handled. But now it seems as though this dropdown is a sort of submit button in itself when AutoPostBack is turned on. So should I figure out how to handle a submit (i.e. differentiate a POST from a GET) and then differentiate between the "Submit" button (which hasn't been created yet, but will be) and the dropdown's AutoPostBack? I suppose that will keep the PageLoad event from firing?
Please forgive me, but I'm learning as I go, so I don't actually know how to do most of this stuff in practice yet, so I'm kinda talking abstractly here. I've worked on Java Servlets and JSP forms for years, but VB and .NET are new to me.
Regards,
Jon Heese
Last edited by jonheese; Oct 20th, 2005 at 11:17 AM.
-
Oct 20th, 2005, 12:19 PM
#4
Thread Starter
New Member
Re: Web form selected reset
Aha, "IsPostBack" is exactly what I was looking for.
Okay, problem solved.
I put a "If Not IsPostBack Then" at the top of the PageLoad sub and now my form behaves exactly as I want. Thanks for all the help... me. =)
Regards,
Jon Heese
-
Oct 20th, 2005, 12:25 PM
#5
Re: Web form selected reset
For your dropdown problem, what's happening currently is that the page_load event gets fired every time, and repopulates your dropdown list. You should place the code for populating the dropdownlist in an if block like this:
VB Code:
If Not Page.IsPostBack Then
'Populate dropdownlist
End If
That'll do wonders
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|