Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setting textfield text to value from drop down list

Status
Not open for further replies.

wallaceoc80

Programmer
Jul 7, 2004
182
0
0
GB
I have a drop down list which is populated from a query to the database in the Page_Load method.

I also have a textfield and I want the value of this to be set to the selected value of the drop down list whenever the user selects a new value.

I have the code:
Code:
tf.setText(ddl.selectedItem.value.ToString());

However, when I select something it always seems to put the value of the first element of the drop down list into the textfield instead of the value selected.

Can anybody help me out here?

Thanks,

Wallace
 
I worked it out. I wasn't checing for postback in the page_load method and I hadn't changed the value of the autopostback for the drop down list to true.

Wallace
 
OK - Steps you will need to do are:

1) Set the AutoPostback to True on the DropDownList

2) On the SelectedIndexChanged method of the DropDownList, set the textbox to the selected item e.g.

Code:
    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        TextBox1.Text = DropDownList1.SelectedItem.Value
    End Sub

If you bind the dropdownlist on the page load, you will need to take this into consideration and don't do the bind on a postback otherwise your selected item will always be the first bound item. e.g.
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            BindData()
        End If
    End Sub
* BindData is the function where you bind the dropdownlist items.

Hope this helps.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Hmmmm... well I don't think that's the fix for my problem... apologies for semi-hijacking your thread wallaceoc80.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top