dynamic button creation with event handling?

P

Peter Vermilye

How can I dynamically create buttons and wire them to an event handler? I
have seen how I can add buttons to an event handler that are pre-created as
part of the codebehind using AddHandler, but I can't use AddHandler with a
button that is created locally, nor can I seem to create multiple button
objects and add them to the page using a button object that exists outside
the procedure I am using.... thus:

Protected WithEvents btnEditIdent As System.Web.UI.WebControls.Button
Protected WithEvents tblDynamic As System.Web.UI.WebControls.Table
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
..
..
..
btnEditIdent = new System.Web.UI.WebControls.Button
btnEditIdent.id="btn" & strSomeUniqueID
dim tc as TableCell
AddHandler btnEditIdent, AddressOf MyButtonHandler
tc.Controls.Add(btnEditIdent)
..
..
..
End Sub
Seems like the above should work. But it generates an error.
Any ideas?

Thanks
Peter R. Vermilye
 
L

Lau Lei Cheong

Seems the code is okay... so what is the error message?

Also make sure you're placing the control inside table within the <form
runat="server"> tag.
 
P

Peter Vermilye

on the "AddHandler btnEditIdent, AddressOf MyButtonHandler" line, the
"btnEditIdent is underlined and the tooltips message I get when I hover over
the button is "'btnEditIdent is not a member of 'MyWeb.MyWebPage'" (where
MyWeb is the name of the application and MyWebPage is the name of the class
for that particular web page.

Yes, the table is inside the <form runat="server"> tag.

Point of clarification: The button is not defined on the web page itself
(in the MyWebPage.aspx file). Should it be? With anything special about it
since I don't intend to use that button but will be using multiple instances
of said button as determined by my code

Thanks for your time in looking at this.

Peter
 
P

Peter Vermilye

I just changed the AddHandler line to read as follows:

AddHandler btnEditIdent.Click, AddressOf MyButtonHandler

the underline went away after that (which NOW makes sense...i wasn't
identifying what event to handle. But in the tutorials I'd looked at, they
never referenced the event itself....I'm going to go test and see if that's
where my stupidity was.

Thanks for your input!

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top