943,783 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 3917
  • VB.NET RSS
Nov 20th, 2007
0

Problem executing function as event from dynamic created linkbuttons

Expand Post »
Hi..

Sorry if this post is misplaced but thourght that it would fit in best here.

What I want to do is call a function, maybe by postback, when a dynamic constructed linkbutton is pushed. This should be done with a parameter and this is the part that I can't figure how to do..

The code I have tried to do this with is:

VB.NET Syntax (Toggle Plain Text)
  1. 1 Partial Class Webform2
  2. 2 Inherits System.Web.UI.Page
  3. 3
  4. 4 Dim linkButton1 As System.Web.UI.WebControls.LinkButton
  5. 5
  6. 6 Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  7. 7
  8. 8 'loop that creates linkbuttons
  9. 9 Dim i = 0
  10. 10 For i = 0 To 5
  11. 11 linkButton1 = New LinkButton()
  12. 12 linkButton1.ID = "TesterLinkbox" + i.ToString
  13. 13 linkButton1.Text = i.ToString
  14. 14 linkButton1.CommandName = "test12"
  15. 15 linkButton1.CommandArgument = "The value i want to pass" 'i.ToString
  16. 16 linkButton1.OnClientClick = "javascript:__doPostBack('LinkButtonTest12','test1');"
  17. 17 Panel1.Controls.Add(linkButton1)
  18. 18 Next
  19. 19
  20. 20
  21. 21 End Sub
  22. 22
  23. 23 ' Did not know if i could use panel-events to handle it..???!
  24. 24
  25. 25 'Protected Sub fisk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.Init
  26. 26 ' MsgBox("Panel initialised")
  27. 27 ' Dim lb As LinkButton = CType(sender, LinkButton)
  28. 28 ' If lb.CommandName = "Fisk" Then
  29. 29 ' MsgBox(lb.CommandArgument)
  30. 30 ' Response.Write(lb.CommandArgument) 'your argument here do any thing you want
  31. 31 ' End If
  32. 32
  33. 33 'End Sub
  34. 34
  35. 35 ' Handles ordinary clicks on a normal linkbutton
  36. 36
  37. 37 Protected Sub Kongen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Kongen.Click
  38. 38 MsgBox("YES")
  39. 39 Dim lb As LinkButton = CType(sender, LinkButton)
  40. 40 If lb.CommandName = "Click" Then
  41. 41 MsgBox(lb.CommandArgument)
  42. 42 Response.Write(lb.CommandArgument)
  43. 43 End If
  44. 44
  45. 45 End Sub
  46. 46
  47. 47 Protected Sub LinkButtonTest12_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButtonTest12.Click
  48. 48 MsgBox("Test12")
  49. 49 Dim lb As LinkButton = CType(sender, LinkButton)
  50. 50 If lb.CommandName = "test12" Then
  51. 51 MsgBox(lb.CommandArgument)
  52. 52 'Response.Write(lb.CommandArgument) 'your argument here do any thing you want
  53. 53 End If
  54. 54 End Sub
  55. 55 End Class
  56. 56
  57. 57




The ASP.NET site looks like this:


VB.NET Syntax (Toggle Plain Text)
  1. 1 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Webform2.aspx.vb" Inherits="Webform2" %>
  2. 2
  3. 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. 4
  5. 5 <html xmlns="http://www.w3.org/1999/xhtml" >
  6. 6 <head runat="server">
  7. 7 <title>Untitled Page</title>
  8. 8 </head>
  9. 9 <body>
  10. 10 <form id="form1" runat="server">
  11. 11 <div>
  12. 12 <asp:LinkButton ID="Kongen" CommandName="Click" CommandArgument="Test" runat="server">Kongen</asp:LinkButton>
  13. 13 <asp:HiddenField ID="Target" runat="server" />
  14. 14
  15. 15 </div>
  16. 16 <asp:HiddenField ID="Argument" runat="server" />
  17. 17 <asp:Panel ID="Panel1" runat="server" Height="331px" Width="511px">
  18. 18 <asp:LinkButton ID="LinkButtonTest12" runat="server">Test12</asp:LinkButton>
  19. 19 </asp:Panel>
  20. 20 </form>
  21. 21 </body>
  22. 22 </html>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sniper1983 is offline Offline
30 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Carriage Return
Next Thread in VB.NET Forum Timeline: merging projects





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC