Surprise with Textbox1.Text

R

Rakesh Parekh

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
 
A

Alessandro Zifiglio

hi Rakesh, use an IsPostBack check in your page_load to add items only if
its not a postback situation.
Postback events are raised after your page_load method(so after you added
items in your dropdownlist,which clears the selected item before the
selectedIndexChanged method executes).

Also your test case is not so clear, you basically keep adding the same
items to your ddl in page_load which fires everytime an item is selected(and
keeps adding to the already existing items in your dropdownlist, in this
case australia,belgium,canada and india keep getting duplicated each time a
postback occurs, unless you disabled viewstate on the dropdownlist. And if
you did then i suggest you turn it back on, and use the ispostback check
instead to populate the items only the first time. With viewstate disabled
the selectedindex changed event wont fire. It needs viewstate enabled to
know if the index has changed or not comparing oldvalue with new. With
viewstate disabled it wont know what the old value was =P
Have a good day,
Alessandro Zifiglio
 
R

Rakesh Parekh

Dear Alessandro,

Thank you very much for your prompt response. I already checked
using IsPostBack at the page_load event. It's not working. I also checked
with EnableViewState=True and also with Falst. It is not working either. OK,
for
the moment let us forget to get the text/value of DropDown list to Textbox on
selectedIndexChanged event.

Even the following or any other TextBox property is working.

Private Sub DropDownList1_SelectedIndexChanged() 'For ease of reading
removed arguments.
TextBox1.BackColor = System.Drawing.Color.Red
End Sub

The TextBox1 control is not at all getting affected with any property
selection
on SelectedIndexChanged event. This event is not able to do anything to the
TextBox
control. Till today I spent about three days on this "too simple code"
without
success and I am very frustrated. Please help me.

Kind regards,
Rakesh
 
A

Alessandro Zifiglio

Rakesh, Seems like the event is not firing at all, verify this. It might be
the way you hooked up the event, probably done it wrongly.

looking at your code and since its a private method it does not seem like
you hooked it up via declarative code. So you are missing the implements
parts :
FOllowing is your Event in your codebehind :
Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
TextBox1.Text = DropDownList1.SelectedItem.Text
End Sub

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

Note the extra parts you are missing in your method -->> Handles
DropDownList1.SelectedIndexChanged

This is what binds the SelectedIndexChanged method to DropDownList1, without
this your event will fire blank coz when your code runs the dropdownlist
wont know what method is going to handle its SelectedIndexChanged event.
Normally this code is automatically added for you by the IDE. Now it should
be working.
Have a good day,
Alessandro Zifiglio
 
G

groovyghoul

Hi

Have you made sure that AutoPostBack is set to True on your
DropDownList? Also wrap your page_load code like this (sorry, c#)

if (!Page.IsPostBack)
{
DropDownList1.Items.Add("Australia");
DropDownList1.Items.Add("Belgium");
DropDownList1.Items.Add("Canada");
DropDownList1.Items.Add("India") ;
}

- Richard
 
R

Rakesh Parekh

Alessandro, the handles selectedIndexchanged is automatically added
by IDE so that was not the problem.

Thank you both Alessandro & Richard. The problem was with
AutoPostback which is by default false. I turned it true
and it works. The viewState is also required to be true. But
now with both these properties to be true I am put in another
problem surely because of them.

If not IsPostBack then I populate the dropdownlist with a
commandtext query to get only DISTINCT country names. It populates
the dropdown list but the values filled in twice though I use
distinct in query. Means, the dropdownlist shows values as under
when I run the page.

Australia
Belgium
Canada
India
Australia
Belgium
Canada
India

Why it fills the values twice. As per me the query runs perfect but
it is because of viewstate or autopost back property it fills value
twice. I tried all alternatives without success. Can you pls help.

Kind regards,
Rakesh
 
G

groovyghoul

Hi Rakesh

Can you confirm that you have wrapped the code the populates the
dropdownlist with a check for IsPostBack. If you do not, then you will
populate everytime a postback is performed. Also, just for kicks, make
sure that you are not populating that ddl anywhere else in your code.
If you want, why don't you post the code block that you are using to
populate and we'll take a quick peek at it.

- Richard
 

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

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top