listbox click - nothing happens?

S

Steve

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
 
K

Karl

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

Karl
 
S

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
 
K

Karl

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
 
S

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
 
K

Karl

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
 
S

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
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top