DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   VB.NET (http://www.daniweb.com/forums/forum58.html)
-   -   Problem executing function as event from dynamic created linkbuttons (http://www.daniweb.com/forums/thread97610.html)

sniper1983 Nov 20th, 2007 4:24 am
Problem executing function as event from dynamic created linkbuttons
 
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:

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




The ASP.NET site looks like this:


1    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Webform2.aspx.vb" Inherits="Webform2" %>
2   
3    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4   
5    <html xmlns="http://www.w3.org/1999/xhtml" >
6    <head runat="server">
7        <title>Untitled Page</title>
8    </head>
9    <body>
10      <form id="form1" runat="server">
11      <div>
12          <asp:LinkButton ID="Kongen" CommandName="Click" CommandArgument="Test" runat="server">Kongen</asp:LinkButton>
13          <asp:HiddenField ID="Target" runat="server" />
14     
15      </div>
16          <asp:HiddenField ID="Argument" runat="server" />
17          <asp:Panel ID="Panel1" runat="server" Height="331px" Width="511px">
18          <asp:LinkButton ID="LinkButtonTest12" runat="server">Test12</asp:LinkButton>
19          </asp:Panel>
20      </form>
21  </body>
22  </html>


All times are GMT -4. The time now is 5:13 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC