Surprise with Textbox1.Text

G

Guest

To my great surprise the following code is not working. I expect
TextBox1.Text to be
changed each time the dropdownlist selected index changes. It works in
Windows application but
surprisingly not not WebApplication web form.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
DropDownList1.Items.Add("Australia")
DropDownList1.Items.Add("Belgium")
DropDownList1.Items.Add("Canada")
DropDownList1.Items.Add("India")
End Sub

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

Pls advise what's wrong with the code.

Thank you,
Rakesh
 
G

Guest

You can't expect a web application to work the same as a windows application.

You have to set the AutoPostBack option of the dropdown list, so that it
will reload the page from the server when the selected index changes.
 
A

AMDRIT

Postbacks are expensive, especially if the server doesn't care that the
dropdown selected value has changed. Simple client side javascript can
provide you the same thing without postback.

set the onSelectedChange property of the dropdown = "fnIchanged();"

create a script (directly in the HTML, or write it out prior to rendering.)

<script language="javascript" ...>
function fnIchanged()
{
TextBox1.value =
DropDownlist1.options[DropDownlist1.SelectedIndex].value;
}
</script>

Posting back to the server on every value change, while easier to code for,
is still extra round trips over the wire. When your client can do the work
for you, let the client do the work, preserve your bandwidth, memory and CPU
for the hard stuff.
 
G

Guest

So correct. Valuable input. :)

That's the reason AutoPostback is not on by default.

Some corrections and modernisations of the Javascript, though:

<script type="text/javascript" ...>
function fnIchanged()
{
document.getElementById('TextBox1').value =
document.getElementById('DropDownlist1').options[document.getElementById('DropDownlist1').selectedIndex].value;
}
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top