1

hello i am trying to pass parameters from asp LinkButton to a sub routine and it doesn't work

this is the linkbutton

<asp:LinkButton runat="server" ID="linkId" OnClick="doSomething(10)">ID
</asp:LinkButton>

and this is my sub

   Public Sub doSomething(ByVal mon As Integer)
      MsgBox("this is the number : " & mon)
   End Sub

can some one tell me how can we make this work in VB.net

Mina Gabriel
  • 23,150
  • 26
  • 96
  • 124
  • you need to specify the sender and event arguments, go look at this question http://stackoverflow.com/questions/7247488/asplinkbutton-not-functioning-in-production – Keith Beard May 22 '12 at 12:28

3 Answers3

5

Try with CommandArgument property

 <asp:LinkButton runat="server" ID="linkId" OnClick="doSomething"
    CommandArgument="10">ID</asp:LinkButton>

and

  Public Sub doSomething(sender As Object, e As EventArgs)
      Dim btn As LinkButton = DirectCast(sender, LinkButton )
      Dim msg As String = "this is the number : " & Int32.Parse(btn.CommandArgument)
  End Sub
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • this works fine , but is there is any way easier cause i don't really understand what you just did ... a way to call it like function(par) what if i have more than one parameter how do you do this – Mina Gabriel May 22 '12 at 12:38
  • 1
    @user1410185: unfortunately you can't do in easier. If you have more than 1 parameter you could do something ugly like having a comma separated string contained different values `CommandArgument="10,mysecondparameter"`. Then you'd have to split the string on the server – Claudio Redi May 22 '12 at 12:41
  • @ClaudioRedi: Sorry for the last rollbacked edit, i thought that the CommandArgument property returns object instead of string(like [CommandEventArgs.CommandArgument](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.commandeventargs.commandargument.aspx)). – Tim Schmelter May 22 '12 at 12:52
  • WAWW i though vb is easier than this – Mina Gabriel May 22 '12 at 13:00
  • @user1410185: things will be easier when you get used to it :) – Claudio Redi May 22 '12 at 13:13
  • well the problem is when you ask a question there is like millions of ways to do it ... which makes you very confused ha we will see :) – Mina Gabriel May 22 '12 at 13:21
1

You cannot pass anything directly to an event handler. But you can use LinkButton's CommandArgument property:

<asp:LinkButton runat="server" 
       ID="linkId" 
       OnClick="LinkButton_Click"
       CommandArgument="10">ID
</asp:LinkButton>

in your codebehind (with the correct signature for this event):

Protected Sub LinkButton_Click(sender As Object, e As EventArgs) 
     Dim lb = DirectCast(sender, LinkButton)
     Dim ID As String = lb.CommandArgument;
End Sub

Note that you should not use a MessageBox in an ASP.NET environment since it would be shown on the server and not in the browser.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • thanks Tim but what if i need to send more than one parameter ? – Mina Gabriel May 22 '12 at 12:42
  • 1
    @user1410185: What could that be? What can be on the client that is not on the server? Normally you only need an identifier which you can pass per id. If you did something on clientside via javascript and you want to persist it, use a HiddenField with runat=server, then you can access it also on serverside. – Tim Schmelter May 22 '12 at 12:45
  • how do I pass a variable x to be value of CommandArgument. my control is not databound? – Muhammad Adeel Zahid Jan 08 '16 at 10:05
  • Ask a real question on stackoverflow, include all necessary informations like your code. – Tim Schmelter Jan 08 '16 at 10:16
1

You don't have to use the DirectCast method. It may make more sense for your sub to look like this:

Public Sub doSomething(sender As Object, e As CommandEventArgs)
  Dim msg As String = "this is the number : " & e.CommandArgument
End Sub

You can also store a second value in the CommandName property of the control and use it the same way.

edit: With a slight change to your control, use OnCommand instead of OnClick:

<asp:LinkButton runat="server" ID="linkId" OnCommand="doSomething"
CommandArgument="10" Text="ID" />
gbusman
  • 167
  • 1
  • 1
  • 7