Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to define the Event handler for programatically created button

Status
Not open for further replies.

bstineman

Programmer
Mar 19, 2003
9
US
Ok, php guy who's *trying* to make the switch to ASP/VB.NET. Running into the normal snags, but all in all I'm muddling through. Currently however I'm hung up on how to add/specify the event handler for a programmatically created button.

According to MSDN I'm supposed to be able to identify the event handler function name in an OnCommand property/attribute for a button control. However, I can't seem to find such a public property/attribute in the Button's member list (there is a protected property by that name).

Is there a way to do this without creating a new button class that exposes a public OnCommand method and then calls the base class's method?

So far I'm finding a large number of tasks I used to be able to do easily in PHP to be difficult in ASP.NET. This is just another one of the straws on this camel's back. :)

Any help is GREATLY appreciated.
 
I think once you get through the initial hardaches of learning a new technology, you'll see that the object oriented nature of ASP.NET alone will make it worth the switch. The more projects I do, the less I have to work on subsequent projects due to the ease of reuseability.

At any rate, you can add an event handler to a button with the following bit of code:

myButton.Click += new System.EventHandler(this.EventHandlerMethodName);

Where EventHandlerMethodName is the name of the method that has the following signature:

protected void EventHandlerMethodName(object o, System.EventArgs e){
//logic here
}

You're using delegates here, which is another very cool feature of .NET. It makes event raising/handling (as well as other tasks) very easy whereas they used to be much more complicated. I would suggest reading up on delegates. Professional C# by WROX Press (which has gone belly up, so the book should be cheap) is an excellent reference.

hth! :)
paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Thanks Link9, but unfortunately that code doesn't appear to be viable when using VB.NET for the code behind.
 
The specific code... no, but the method, yes:

I can tell you this, though. Programatically adding controls & events in VB is buggy. I used VB.NET when I started w/ .NET, and it was this specific buggy behavior that finally made me throw my hands up w/ the language and move to C#.

Sometimes it works, and sometimes it doesn't. The same code works on the first click sometimes, and other times, it takes a second post back before the event handler will fire.

There's been much discussion on this subject, so I won't go into it here, but a quick search on Google will find you the translated code. I hesitate to offer it, and instead submit that if you're using this design method, you're really going to need to go w/ C#. You'll thank yourself.

And the switch... very easy. The classes are the same, it's only the syntax you'll need to get used to. I would suggest to anyone learning .NET to just go w/ C#. It's a better language, so why not learn the superior of the two if you're on a learning curve, anyway?

-paul
penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thanks Link9, I'll do the reading on the method and see what I can dig up. As or changing to c#, that's not an option. The company I work for is already strongly entrenched in VB and moved to .NET so that the web side (which I'm being asked to help expand) uses the same language (at least as closely as .NET will allow) and methods and our internal, traditional, legacy applications.

In all honesty, if I had my choice, I'd be doing the presentation layers in LAMP (linux/apache/mySQL/php) with middleware applications being developed as web services in another language such as C++ or VB (vb only because we've got 4+ developers here that know the business well and know VB)
 
Found a solution...

AddHandler btnRemoveLoan.Click, AddressOf btnRemoveLoan_Click

This allowed me to easily assign an event handler to my programatic button. It accomplishes exactly what I wanted to do with one simple line. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top