Surprise with Textbox1.Text

Discussion in 'ASP .Net Web Controls' started by Rakesh Parekh, Apr 25, 2006.

  1. 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
    Rakesh Parekh, Apr 25, 2006
    #1
    1. Advertising

  2. 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

    "Rakesh Parekh" <> ha scritto nel
    messaggio news:...
    > 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
    Alessandro Zifiglio, Apr 25, 2006
    #2
    1. Advertising

  3. 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

    "Alessandro Zifiglio" wrote:

    > 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
    >
    > "Rakesh Parekh" <> ha scritto nel
    > messaggio news:...
    > > 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

    >
    >
    >
    Rakesh Parekh, Apr 26, 2006
    #3
  4. 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

    "Rakesh Parekh" <> ha scritto nel
    messaggio news:...
    > 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
    >
    > "Alessandro Zifiglio" wrote:
    >
    >> 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
    >>
    >> "Rakesh Parekh" <> ha scritto nel
    >> messaggio news:...
    >> > 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

    >>
    >>
    >>
    Alessandro Zifiglio, Apr 26, 2006
    #4
  5. Rakesh Parekh

    groovyghoul Guest

    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
    groovyghoul, Apr 27, 2006
    #5
  6. 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


    "groovyghoul" wrote:

    > 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
    >
    >
    Rakesh Parekh, Apr 28, 2006
    #6
  7. Rakesh Parekh

    groovyghoul Guest

    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
    groovyghoul, Apr 29, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. al
    Replies:
    7
    Views:
    4,059
    George
    Jun 17, 2004
  2. =?Utf-8?B?YWU=?=

    TextBox1.text = "test";

    =?Utf-8?B?YWU=?=, Sep 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    3,920
    =?Utf-8?B?YWU=?=
    Sep 23, 2005
  3. Replies:
    2
    Views:
    374
  4. =?Utf-8?B?cm9kY2hhcg==?=

    Edit DataBindings for TextBox1

    =?Utf-8?B?cm9kY2hhcg==?=, Dec 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    277
    =?Utf-8?B?cm9kY2hhcg==?=
    Dec 4, 2006
  5. Replies:
    1
    Views:
    1,022
    =?Utf-8?B?TWFuaXNoIEJhZm5h?=
    Feb 4, 2007
Loading...

Share This Page