ASP.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a ASP.NET Expert!

Join 307,150 ASP.NET Programmers for FREE! Get instant access to thousands of ASP.NET experts, tutorials, code snippets, and more! There are 1,705 people online right now. Registration is fast and FREE... Join Now!




Dynamically change button action

 

Dynamically change button action

W3bDev

18 Aug, 2009 - 10:21 AM
Post #1

D.I.C Head
Group Icon

Joined: 15 Mar, 2009
Posts: 207



Thanked: 20 times
Dream Kudos: 50
My Contributions
Hello All,

I'm working on an app that does some dynamic selection within a form, and I need some help with a button. Basically, when a certain condition is met, I would like the button action to change. What do I mean? So basically, the general action performed when the button is clicked is:

CODE

protected void myButton_Click(object sender, EventArgs e){
// some code here
}


Works fine and dandy, but when something happens elsewhere on the form (I am using autopostback and asp.net ajax), I would like the destination method changed from void myButton_Click, to a custom method of my choosing. What's the easiest way to do this? Thank you for your help!

User is offlineProfile CardPM
+Quote Post


JackOfAllTrades

RE: Dynamically Change Button Action

18 Aug, 2009 - 10:25 AM
Post #2

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 5,324



Thanked: 454 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
This might better off in ASP.NET, but usually you would change the event handling delegation to point to some other code. So something like:
csharp

myButton.OnClick += myEventHandler;

//Remove old handler
myButton.OnClick -= myEventHandler;

// Add new one
myButton.OnClick += myNewEventHandler;


I'll move this to ASP.NET for further discussion.
User is offlineProfile CardPM
+Quote Post

W3bDev

RE: Dynamically Change Button Action

18 Aug, 2009 - 10:30 AM
Post #3

D.I.C Head
Group Icon

Joined: 15 Mar, 2009
Posts: 207



Thanked: 20 times
Dream Kudos: 50
My Contributions
hmm, I was hoping to handle this at the C# level....
User is offlineProfile CardPM
+Quote Post

W3bDev

RE: Dynamically Change Button Action

18 Aug, 2009 - 11:47 AM
Post #4

D.I.C Head
Group Icon

Joined: 15 Mar, 2009
Posts: 207



Thanked: 20 times
Dream Kudos: 50
My Contributions
Well, I went ahead and came up with a solution due to lack of responses hmmm.gif

Just put another button on the page with a visible = false property, and when the requrements were met, swapping the buttons.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:31PM

Live ASP.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

ASP.NET Tutorials

Reference Sheets

ASP.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month