[thelist] ASP.NET Adding a linkbutton programmatically

Casey Crookston caseyc at IntelliSoftmn.com
Tue Aug 8 07:57:53 CDT 2006


Thanks Peter....

 

Neither the AddHandler or the Attributes.Add are working :-(.  The darn
function just never executes.  I ran through it in de-bug mode, and it
just simply skips it.  No error.

 

As per your comments:  The "ByVal" in lbAlpha_Click is added by Visual
Studio even if I remove them.  If I change "Pubic Sub AlphaCommand" to
Shared, all thee lines within the sub return this error:

 

C:\Inetpub\wwwroot\cmp\pages\WebUserControl1.ascx.vb(103): Cannot refer
to an instance member of a class from within a shared method or shared
member initializer without an explicit instance of the class.

 

Here's what I'm now using:

 

    Private Sub lbCategory_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles lbCategory.Click

        populateAlpha()

    End Sub

 

    Private Sub populateAlpha()

        trAlpha.Visible = True

        trButton.Visible = False

        Dim i As Integer

        For i = 65 To 90

            Dim lbAlpha As LinkButton = New LinkButton

            lbAlpha.Text = "&#" & i & "; "

            lbAlpha.CssClass = "alpha_link"

            lbAlpha.ID = "lbAlpha" & i

            'lbAlpha.Attributes.Add("OnClick", "AlphaCommand()")

            AddHandler lbAlpha.Click, New EventHandler(AddressOf
AlphaCommand)

            plhAlpha.Controls.Add(lbAlpha)

        Next

    End Sub

 

    Public Sub AlphaCommand(ByVal sender As Object, ByVal e As
System.EventArgs)

        trAlpha.Visible = True

        trButton.Visible = False

        lblTest.Text = "Hey There!"

    End Sub

 

 

--------- Original Message --------------------

From: Peter Brunone (EasyListBox.com) [mailto:peter at easylistbox.com] 

Sent: Monday, August 07, 2006 4:35 PM

To: thelist at lists.evolt.org

Cc: Casey Crookston

Subject: RE: [thelist] ASP.NET Adding a linkbutton programmatically

 

   What if you change the assignment slightly like this:

 

AddHandler lbAlpha.Click, New EventHandler(AddressOf lbAlpha_Click)

 

   Does that help?

 

   By the way, you should be able to assign the event handler in your
original way... as long as you use the reference to the sub itself.
Also, you might want to make that sub shared instead of private... not
sure if that will do much, but it's worth a shot... and remove the
"ByVal" designators, since I'm pretty sure the default behavior for
objects is ByRef.

 

   Once you're inside the sub, you can access the linkbutton by ID and
gather property values... or just use the Sender parameter, since that
is a reference to the object (linkbutton) that triggered the event.

 

HTH,

 

Peter

 

From: "Casey Crookston" caseyc at IntelliSoftmn.com 

 

Thanks for the help, Peter. I'm not using any javasscript other then

what is genrated by the LinkButton. Here's what I've got so far.... the

problem seems to be getting Private Sub lbAlpha_Click to fire, and once

it does, how to glean the letter that was clicked.

 

Thanks!

 

Private Sub lbCategory_Click(ByVal sender As System.Object, ByVal e

As System.EventArgs) Handles lbCategory.Click

trAlpha.Visible = True

trButton.Visible = False

populateAlpha()

End Sub

 

Private Sub populateAlpha()

Dim i As Integer

For i = 65 To 90

Dim lbAlpha As LinkButton = New LinkButton

lbAlpha.Text = "&#" & i & "; "

lbAlpha.CssClass = "alpha_link"

lbAlpha.ID = "lbAlpha" & i

'lbAlpha.Attributes.Add("OnClick", "lbAlpha_Click()")

AddHandler lbAlpha.Click, AddressOf lbAlpha_Click

plhAlpha.Controls.Add(lbAlpha)

Next

End Sub

 

Private Sub lbAlpha_Click(ByVal sender As Object, ByVal e As

EventArgs)

trAlpha.Visible = True

trButton.Visible = False

lblTest.Text = "Hey There!"

End Sub

 

 




More information about the thelist mailing list