listbox click - nothing happens?

Discussion in 'ASP .Net' started by Steve, Aug 19, 2004.

  1. Steve

    Steve Guest

    Hi,

    I open up a webform (vb.net) and populate a listbox
    control on the Page load event. If I click on (select)
    and item from the listbox I want to write the value of the
    selected item to a label control. Nothing happens. But
    when I click a button (which contains same code to write
    to label) I get the value of the selected item in the
    Label.

    I looked at the html portion of my webform and don't see a
    listing for the onclick event of the button. So, do I
    need to manually enter the "SelectedIndexChanged" event in
    the html for the listbox?

    <asp:listbox id="lstProducts" style="Z-INDEX: 101; LEFT:
    48px; POSITION: absolute; TOP: 80px"runat="server"
    Height="192px" Width="400px"></asp:listbox>

    Thanks,
    Steve
    Steve, Aug 19, 2004
    #1
    1. Advertising

  2. Steve

    Karl Guest

    Yes, you do. You need to set the listbox's AutoPostBack property to true
    and hook up the SelectedIndexChange event to a method.

    Karl

    "Steve" <> wrote in message
    news:94a901c4863c$a99b3f20$...
    > Hi,
    >
    > I open up a webform (vb.net) and populate a listbox
    > control on the Page load event. If I click on (select)
    > and item from the listbox I want to write the value of the
    > selected item to a label control. Nothing happens. But
    > when I click a button (which contains same code to write
    > to label) I get the value of the selected item in the
    > Label.
    >
    > I looked at the html portion of my webform and don't see a
    > listing for the onclick event of the button. So, do I
    > need to manually enter the "SelectedIndexChanged" event in
    > the html for the listbox?
    >
    > <asp:listbox id="lstProducts" style="Z-INDEX: 101; LEFT:
    > 48px; POSITION: absolute; TOP: 80px"runat="server"
    > Height="192px" Width="400px"></asp:listbox>
    >
    > Thanks,
    > Steve
    Karl, Aug 19, 2004
    #2
    1. Advertising

  3. Steve

    Steve Guest

    Well I modified my listbox tag (which now works) as follows

    <asp:listbox id="lstProducts" style="Z-INDEX: 101; LEFT:
    48px; POSITION: absolute; TOP: 80px" autopostback="True"
    OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"
    runat="server" Height="192px" Width="400px"></asp:listbox>

    I added autopostback="True" and then called the sub
    OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"

    But I had to remove the Private keyword from the sub.
    Question: is it standard convention then to use
    autopostback="true" to invoke subs for aspx? this did
    not seem real intuitive to me.

    Thanks,
    Steve

    >-----Original Message-----
    >Hi,
    >
    >I open up a webform (vb.net) and populate a listbox
    >control on the Page load event. If I click on (select)
    >and item from the listbox I want to write the value of

    the
    >selected item to a label control. Nothing happens. But
    >when I click a button (which contains same code to write
    >to label) I get the value of the selected item in the
    >Label.
    >
    >I looked at the html portion of my webform and don't see

    a
    >listing for the onclick event of the button. So, do I
    >need to manually enter the "SelectedIndexChanged" event

    in
    >the html for the listbox?
    >
    ><asp:listbox id="lstProducts" style="Z-INDEX: 101; LEFT:
    >48px; POSITION: absolute; TOP: 80px"runat="server"
    >Height="192px" Width="400px"></asp:listbox>
    >
    >Thanks,
    >Steve
    >.
    >
    Steve, Aug 20, 2004
    #3
  4. Steve

    Karl Guest

    What you are doing is standard convention. Perhaps they made the default to
    false because (a) it's how things have worked in the past (b) there is a
    performance penalty (you are hitting the server again), so they want you to
    consciouly acknowledge that this is what you are doing.

    You can keep the sub private, and remove the
    OnSelectedIndexChanged="lstProducts_SelectedIndexChanged" from the tag and
    instead declare the lstProducts in your codebehind with the "WithEvents"
    keyword and declare your sub something like

    private sub...() Handles lstProducts.SelectedIndexChange

    Differences are trivial so consider it more a matter of preference.

    Karl

    "Steve" <> wrote in message
    news:94f301c48641$e6bf7600$...
    > Well I modified my listbox tag (which now works) as follows
    >
    > <asp:listbox id="lstProducts" style="Z-INDEX: 101; LEFT:
    > 48px; POSITION: absolute; TOP: 80px" autopostback="True"
    > OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"
    > runat="server" Height="192px" Width="400px"></asp:listbox>
    >
    > I added autopostback="True" and then called the sub
    > OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"
    >
    > But I had to remove the Private keyword from the sub.
    > Question: is it standard convention then to use
    > autopostback="true" to invoke subs for aspx? this did
    > not seem real intuitive to me.
    >
    > Thanks,
    > Steve
    >
    > >-----Original Message-----
    > >Hi,
    > >
    > >I open up a webform (vb.net) and populate a listbox
    > >control on the Page load event. If I click on (select)
    > >and item from the listbox I want to write the value of

    > the
    > >selected item to a label control. Nothing happens. But
    > >when I click a button (which contains same code to write
    > >to label) I get the value of the selected item in the
    > >Label.
    > >
    > >I looked at the html portion of my webform and don't see

    > a
    > >listing for the onclick event of the button. So, do I
    > >need to manually enter the "SelectedIndexChanged" event

    > in
    > >the html for the listbox?
    > >
    > ><asp:listbox id="lstProducts" style="Z-INDEX: 101; LEFT:
    > >48px; POSITION: absolute; TOP: 80px"runat="server"
    > >Height="192px" Width="400px"></asp:listbox>
    > >
    > >Thanks,
    > >Steve
    > >.
    > >
    Karl, Aug 20, 2004
    #4
  5. Steve

    Steve Guest

    Thanks for the explanation. I took a class in asp.net
    last spring (short course). Now I have to start using
    it. May I ask what the WithEvents version of your
    explanation would look like?

    I noticed that I didn't have to deal with this with the
    button. I am guessing the button has different rules
    than a listbox. So for WithEvents


    Dim lst1 As ListBox1 WithEvents

    ....

    Private Sub ListBox1_SelectedIndexChanged(...)
    ....
    End Sub

    something like this? I already have something that
    works, but only in the blind. Just trying to grasp some
    understanding.

    Thanks for your reply,
    Steve


    >-----Original Message-----
    >What you are doing is standard convention. Perhaps they

    made the default to
    >false because (a) it's how things have worked in the

    past (b) there is a
    >performance penalty (you are hitting the server again),

    so they want you to
    >consciouly acknowledge that this is what you are doing.
    >
    >You can keep the sub private, and remove the
    >OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"

    from the tag and
    >instead declare the lstProducts in your codebehind with

    the "WithEvents"
    >keyword and declare your sub something like
    >
    >private sub...() Handles lstProducts.SelectedIndexChange
    >
    >Differences are trivial so consider it more a matter of

    preference.
    >
    >Karl
    >
    >"Steve" <> wrote in

    message
    >news:94f301c48641$e6bf7600$...
    >> Well I modified my listbox tag (which now works) as

    follows
    >>
    >> <asp:listbox id="lstProducts" style="Z-INDEX: 101;

    LEFT:
    >> 48px; POSITION: absolute; TOP: 80px"

    autopostback="True"
    >>

    OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"
    >> runat="server" Height="192px"

    Width="400px"></asp:listbox>
    >>
    >> I added autopostback="True" and then called the sub
    >>

    OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"
    >>
    >> But I had to remove the Private keyword from the sub.
    >> Question: is it standard convention then to use
    >> autopostback="true" to invoke subs for aspx? this did
    >> not seem real intuitive to me.
    >>
    >> Thanks,
    >> Steve
    >>
    >> >-----Original Message-----
    >> >Hi,
    >> >
    >> >I open up a webform (vb.net) and populate a listbox
    >> >control on the Page load event. If I click on

    (select)
    >> >and item from the listbox I want to write the value of

    >> the
    >> >selected item to a label control. Nothing happens.

    But
    >> >when I click a button (which contains same code to

    write
    >> >to label) I get the value of the selected item in the
    >> >Label.
    >> >
    >> >I looked at the html portion of my webform and don't

    see
    >> a
    >> >listing for the onclick event of the button. So, do I
    >> >need to manually enter the "SelectedIndexChanged"

    event
    >> in
    >> >the html for the listbox?
    >> >
    >> ><asp:listbox id="lstProducts" style="Z-INDEX: 101;

    LEFT:
    >> >48px; POSITION: absolute; TOP: 80px"runat="server"
    >> >Height="192px" Width="400px"></asp:listbox>
    >> >
    >> >Thanks,
    >> >Steve
    >> >.
    >> >

    >
    >
    >.
    >
    Steve, Aug 20, 2004
    #5
  6. Steve

    Karl Guest

    The button would be the same...it's a VB.Net thing, not a specific control
    thing. It has to do with how to hook up events.

    If you go to your codebehind page and expand the section which was created
    by the designer. Chances are you'll see all the controls you use in your
    aspx file. They'll already have the WithEvents on them, and look something
    like:

    Protected WithEvents lst1 as System.Web.UI.WebControls.ListBox
    Protected WithEvents ....

    If they aren't there, make sure to check closely, you can add them.

    One that's done, it looks like this:

    Private Sub Doesn_not_Matter_what_Name(...) Handles
    lst1.SelectedIndexChanged
    ....
    End Sub


    "Steve" <> wrote in message
    news:264601c48659$40bc9a90$...
    > Thanks for the explanation. I took a class in asp.net
    > last spring (short course). Now I have to start using
    > it. May I ask what the WithEvents version of your
    > explanation would look like?
    >
    > I noticed that I didn't have to deal with this with the
    > button. I am guessing the button has different rules
    > than a listbox. So for WithEvents
    >
    >
    > Dim lst1 As ListBox1 WithEvents
    >
    > ...
    >
    > Private Sub ListBox1_SelectedIndexChanged(...)
    > ...
    > End Sub
    >
    > something like this? I already have something that
    > works, but only in the blind. Just trying to grasp some
    > understanding.
    >
    > Thanks for your reply,
    > Steve
    >
    >
    > >-----Original Message-----
    > >What you are doing is standard convention. Perhaps they

    > made the default to
    > >false because (a) it's how things have worked in the

    > past (b) there is a
    > >performance penalty (you are hitting the server again),

    > so they want you to
    > >consciouly acknowledge that this is what you are doing.
    > >
    > >You can keep the sub private, and remove the
    > >OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"

    > from the tag and
    > >instead declare the lstProducts in your codebehind with

    > the "WithEvents"
    > >keyword and declare your sub something like
    > >
    > >private sub...() Handles lstProducts.SelectedIndexChange
    > >
    > >Differences are trivial so consider it more a matter of

    > preference.
    > >
    > >Karl
    > >
    > >"Steve" <> wrote in

    > message
    > >news:94f301c48641$e6bf7600$...
    > >> Well I modified my listbox tag (which now works) as

    > follows
    > >>
    > >> <asp:listbox id="lstProducts" style="Z-INDEX: 101;

    > LEFT:
    > >> 48px; POSITION: absolute; TOP: 80px"

    > autopostback="True"
    > >>

    > OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"
    > >> runat="server" Height="192px"

    > Width="400px"></asp:listbox>
    > >>
    > >> I added autopostback="True" and then called the sub
    > >>

    > OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"
    > >>
    > >> But I had to remove the Private keyword from the sub.
    > >> Question: is it standard convention then to use
    > >> autopostback="true" to invoke subs for aspx? this did
    > >> not seem real intuitive to me.
    > >>
    > >> Thanks,
    > >> Steve
    > >>
    > >> >-----Original Message-----
    > >> >Hi,
    > >> >
    > >> >I open up a webform (vb.net) and populate a listbox
    > >> >control on the Page load event. If I click on

    > (select)
    > >> >and item from the listbox I want to write the value of
    > >> the
    > >> >selected item to a label control. Nothing happens.

    > But
    > >> >when I click a button (which contains same code to

    > write
    > >> >to label) I get the value of the selected item in the
    > >> >Label.
    > >> >
    > >> >I looked at the html portion of my webform and don't

    > see
    > >> a
    > >> >listing for the onclick event of the button. So, do I
    > >> >need to manually enter the "SelectedIndexChanged"

    > event
    > >> in
    > >> >the html for the listbox?
    > >> >
    > >> ><asp:listbox id="lstProducts" style="Z-INDEX: 101;

    > LEFT:
    > >> >48px; POSITION: absolute; TOP: 80px"runat="server"
    > >> >Height="192px" Width="400px"></asp:listbox>
    > >> >
    > >> >Thanks,
    > >> >Steve
    > >> >.
    > >> >

    > >
    > >
    > >.
    > >
    Karl, Aug 20, 2004
    #6
  7. Steve

    Steve Guest

    Thanks. I get it now. I forgot about that region
    designer thing. Now I have a little more understanding of
    what I am doing. I don't like "just getting lucky". Luck
    usually runs out (quickly for me :).

    Thanks for your help

    >-----Original Message-----
    >The button would be the same...it's a VB.Net thing, not a

    specific control
    >thing. It has to do with how to hook up events.
    >
    >If you go to your codebehind page and expand the section

    which was created
    >by the designer. Chances are you'll see all the controls

    you use in your
    >aspx file. They'll already have the WithEvents on them,

    and look something
    >like:
    >
    >Protected WithEvents lst1 as

    System.Web.UI.WebControls.ListBox
    >Protected WithEvents ....
    >
    >If they aren't there, make sure to check closely, you can

    add them.
    >
    >One that's done, it looks like this:
    >
    >Private Sub Doesn_not_Matter_what_Name(...) Handles
    >lst1.SelectedIndexChanged
    >....
    >End Sub
    >
    >
    >"Steve" <> wrote in

    message
    >news:264601c48659$40bc9a90$...
    >> Thanks for the explanation. I took a class in asp.net
    >> last spring (short course). Now I have to start using
    >> it. May I ask what the WithEvents version of your
    >> explanation would look like?
    >>
    >> I noticed that I didn't have to deal with this with the
    >> button. I am guessing the button has different rules
    >> than a listbox. So for WithEvents
    >>
    >>
    >> Dim lst1 As ListBox1 WithEvents
    >>
    >> ...
    >>
    >> Private Sub ListBox1_SelectedIndexChanged(...)
    >> ...
    >> End Sub
    >>
    >> something like this? I already have something that
    >> works, but only in the blind. Just trying to grasp some
    >> understanding.
    >>
    >> Thanks for your reply,
    >> Steve
    >>
    >>
    >> >-----Original Message-----
    >> >What you are doing is standard convention. Perhaps

    they
    >> made the default to
    >> >false because (a) it's how things have worked in the

    >> past (b) there is a
    >> >performance penalty (you are hitting the server again),

    >> so they want you to
    >> >consciouly acknowledge that this is what you are doing.
    >> >
    >> >You can keep the sub private, and remove the

    >>

    >OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"
    >> from the tag and
    >> >instead declare the lstProducts in your codebehind with

    >> the "WithEvents"
    >> >keyword and declare your sub something like
    >> >
    >> >private sub...() Handles

    lstProducts.SelectedIndexChange
    >> >
    >> >Differences are trivial so consider it more a matter of

    >> preference.
    >> >
    >> >Karl
    >> >
    >> >"Steve" <> wrote in

    >> message
    >> >news:94f301c48641$e6bf7600$...
    >> >> Well I modified my listbox tag (which now works) as

    >> follows
    >> >>
    >> >> <asp:listbox id="lstProducts" style="Z-INDEX: 101;

    >> LEFT:
    >> >> 48px; POSITION: absolute; TOP: 80px"

    >> autopostback="True"
    >> >>

    >>

    OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"
    >> >> runat="server" Height="192px"

    >> Width="400px"></asp:listbox>
    >> >>
    >> >> I added autopostback="True" and then called the

    sub
    >> >>

    >>

    OnSelectedIndexChanged="lstProducts_SelectedIndexChanged"
    >> >>
    >> >> But I had to remove the Private keyword from the sub.
    >> >> Question: is it standard convention then to use
    >> >> autopostback="true" to invoke subs for aspx? this

    did
    >> >> not seem real intuitive to me.
    >> >>
    >> >> Thanks,
    >> >> Steve
    >> >>
    >> >> >-----Original Message-----
    >> >> >Hi,
    >> >> >
    >> >> >I open up a webform (vb.net) and populate a listbox
    >> >> >control on the Page load event. If I click on

    >> (select)
    >> >> >and item from the listbox I want to write the value

    of
    >> >> the
    >> >> >selected item to a label control. Nothing happens.

    >> But
    >> >> >when I click a button (which contains same code to

    >> write
    >> >> >to label) I get the value of the selected item in

    the
    >> >> >Label.
    >> >> >
    >> >> >I looked at the html portion of my webform and don't

    >> see
    >> >> a
    >> >> >listing for the onclick event of the button. So,

    do I
    >> >> >need to manually enter the "SelectedIndexChanged"

    >> event
    >> >> in
    >> >> >the html for the listbox?
    >> >> >
    >> >> ><asp:listbox id="lstProducts" style="Z-INDEX: 101;

    >> LEFT:
    >> >> >48px; POSITION: absolute; TOP: 80px"runat="server"
    >> >> >Height="192px" Width="400px"></asp:listbox>
    >> >> >
    >> >> >Thanks,
    >> >> >Steve
    >> >> >.
    >> >> >
    >> >
    >> >
    >> >.
    >> >

    >
    >
    >.
    >
    Steve, Aug 20, 2004
    #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. John Saunders

    Re: No Error and nothing happens!

    John Saunders, Jun 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    324
    Kevin Spencer
    Jun 30, 2004
  2. =?Utf-8?B?bWF4ZmxvZGVu?=

    Nothing happens when I click a button

    =?Utf-8?B?bWF4ZmxvZGVu?=, Jul 16, 2005, in forum: ASP .Net
    Replies:
    14
    Views:
    5,950
    =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?=
    Feb 6, 2006
  3. Aubrey Jones
    Replies:
    0
    Views:
    500
    Aubrey Jones
    Aug 11, 2004
  4. Torben Birk
    Replies:
    2
    Views:
    509
    Torben Birk
    Sep 5, 2006
  5. NM
    Replies:
    6
    Views:
    449
    Default User
    Sep 20, 2006
Loading...

Share This Page