I have a page that has a dropdown list of customers. I want to select a customer from that dropdown and populate the appropriate textboxes.
So when I select 'John Smith' from the dropdown list I have a form with textboxes for first name, last name, accout id, etc that needs to populate accordingly. I am using VB.net with MS SQL. I have been successful populating the dropdown list from the DB. Where I am lost is populating the appropriate textboxes for the customer selected in the dropdown list. Any help is appreciated.Hi,
If you are having the dropdownlist and the textbox in the same form, then you need to do as below:-
Set the autopostback property for the dropdownlist to true. This will make the dropdownlist post back to the server, once an element is selected. Then, you can put the following code in your codebehind file to get the selected item.
If Not IsPostBack Then
textbox1.text = dropdownlist1.SelectedItem.Text
End If
If you are having the textbox in a different form, then you need to pass the selected value by querystring to that form.
If Not IsPostBack Then
Response.Redirect("NewForm.aspx?name=" & dropdownlist1.SelectedItem.Text)
End If
Then in the new form, you can get the value using the following code:-
textbox1.text = Request.Params("name")
Hope it helps.
Something to keep in mind:
The name of the user (or whatever) may not be unique. You can have two people with the name "John Smith." You propably have a column in your DB that is unique for each person like UserID or whatever.
So when you update the dropdownlist from the database, you need to retrieve the unique id for each person as well, so that when the index of the selected index in the drop down list changes, you can use the corresponding unique ID to retrieve the other info from the database.
I cant remember exactly, but you should take a loot at the dropdown control itself. Many such controls have a 'Value' for each item in them. You van set the 'Value' to the unique ID somehow.
Youll have to take a look...
0 comments:
Post a Comment