Thursday, March 22, 2012

listbox click - nothing happens?

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,
SteveYes, you do. You need to set the listbox's AutoPostBack property to true
and hook up the SelectedIndexChange event to a method.

Karl

"Steve" <anonymous@.discussions.microsoft.com> wrote in message
news:94a901c4863c$a99b3f20$a501280a@.phx.gbl...
> 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
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
>.
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" <anonymous@.discussions.microsoft.com> wrote in message
news:94f301c48641$e6bf7600$a501280a@.phx.gbl...
> 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
> >.
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" <anonymous@.discussions.microsoft.com> wrote in
message
>news:94f301c48641$e6bf7600$a501280a@.phx.gbl...
>> 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
>> >.
>>
>.
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" <anonymous@.discussions.microsoft.com> wrote in message
news:264601c48659$40bc9a90$a301280a@.phx.gbl...
> 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" <anonymous@.discussions.microsoft.com> wrote in
> message
> >news:94f301c48641$e6bf7600$a501280a@.phx.gbl...
> >> 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
> >> >.
> >> >.
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" <anonymous@.discussions.microsoft.com> wrote in
message
>news:264601c48659$40bc9a90$a301280a@.phx.gbl...
>> 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" <anonymous@.discussions.microsoft.com> wrote in
>> message
>> >news:94f301c48641$e6bf7600$a501280a@.phx.gbl...
>> >> 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
>> >> >.
>> >>>>> >.
>>
>.

0 comments:

Post a Comment