SharePoint Webpart (not visial webpart) VB.net (2010) I have list box that the user choose an item the click the button. The button will take the selectemitem.text and and create the output. in VB (asp) page that I am converting from this was handled by:

Protected Sub LBaddgrp_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Click_btnadd

in the webpart the "Handles Click_btnadd" it gives error handles clause requires with events variable

So how Do I do this in a webpart? I can't seem to find a example online.


Your Method Signature looks all wrong.

Your Method is called LBaddgrp_SelectedIndexChanged while it Handles Click_btnadd

Now I'm no VB expert, but shouldn't it be more like :

Protected Sub ListBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

End Sub

It seems you are handling the incorrect Event there mate.

What I think you are trying to achieve is this :

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    value = ListBox1.SelectedValue
End Sub

Hope this helps.

  • it appears the SharePoint Webpart has limitation on what Handles can be for a sub. The above solutions didn't work, I do appreciate your answer. – Robert Feb 23 '12 at 12:45
  • 1
    Sharepoint web part has no limitations as it is a stock standard ASP.NET control (ASCX). Your coding is wrong. You cannot handle two events at the same time. You are trying to handle ListBox SelectedIndexChanged as well as Button Click. If you think about it, that doesn't even make sense. A User cannot select another item in a Listbox and click a button at the same time! Also, I suggest you switch over to C#.. In my view, VB is old and outdated (I'm going to be nailed for that comment) – Fox Feb 24 '12 at 7:02
  • I have heard C# is better but this is what I know and not that much. What is best way to get the selectitem.text to a button click? – Robert Feb 24 '12 at 13:08
  • ok I figured it out I just need to have a label or text box not visible to store the value. for the button click. Now the new issue is why cant i have more than one SelectedIndexChanged... oh well searching i go. Thank you again. – Robert Feb 24 '12 at 19:25
  • 1
    Hi Robert. SelectedindexChanged is an Event. It gets fired when the Selected Index (The listitem) changes. The code snipped i shows you above allows you to get the selected item (or its value) when you click the button. Apologies for being abrupt my friend, but you dont really have a problem with sharepoint, but more a lack of coding skills (ASP.NET) in particular. I suggest you go and read some more development tutorials, but my suggestion is to forget what you know about Visual Basic.NET and start a frash with C#. – Fox Feb 27 '12 at 8:25

Do you want the user to be able to select an item from a list, as well as click a button (that is, allow to the user to do either), and have the same thing happen?

If so, you will need to use two separate event handlers, exactly as Fox has written here, but you need to have these two event handlers call a separate method which does the logic you want to do.

Essentially, the 'event' of selecting an item, or clicking the button, will call the extra new method that does what you want to do, via each 'events' respective event handlers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.