Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 21st, 2010, 02:58 PM
Authorized User
 
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
Default How to check if Button is Clicked in ASP?

I have two buttons on the page... How can I find out which Button was clicked, in the Page_Load event?

I tried:
i have placed a label control which has empty Text property.
And when Button is Clicked, I have given some text in the Button_click event.


So when Button is pressed, i thought the page_load check the text property of the label and so detect the Button is Pressed.

But The problem is that when the button is clicked, the page goes to the Page_Load first, before it gets to the click event handlers. so it could not gets its text property.

Is there any way in the page load that I can check to see if the button has been clicked or any way to force the button to run an event handler sub before it runs the page load?
 
Old May 21st, 2010, 04:02 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Why not just move the code from the Page_Load into a method of its own, and call it from the two button click events?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old May 22nd, 2010, 03:29 PM
Authorized User
 
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Even if i move the code to seperate method and call it, anyway if i click the Button, it goes to page_load first before it goes to click event or any other method.

In page_load i am calling other functions, which i dont want to execute if the Button is clicked. And thats the reason i want to check it from Page_load if Button is clicked, if it does then i dont want to call the function, otherwise that function is called.
 
Old May 23rd, 2010, 02:33 PM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

This is a problem where you just need to figure out what goes where. It sounds like you have all the pieces it's a challenge putting them together. Without code samples, I can't tell for sure, but I suspect you've set your code up like a lot of the MSDN samples...

Code:
... Page_Load(...)
{
   // Do stuff everytime page loads


   if (!IsPostBack)
   {
      // Only do stuff when page loads the FIRST time
   }
   else
   {
      // Only do stuff when page loads the second time (or third, fourth, etc.)
   }

}
That's fine when you only have one button on the page. You use the if statement for decision making and you test the IsPostBack property of the page to determine if this is the first time the page has posted back or not. Note that you can do things EVERY time the page loads, they just go outside that if-else test. However, you are only testing the postback property. You can't tell this way which button

However, you have two buttons. You need to use an alternate code organization...


Code:
... Page_Load(...)
{
   // Do stuff everytime page loads
}

... Button1_click(...)
{
   // Do stuff only when the page posts back because button ONE was clicked. You can write all your code here, or you can encapsulate parts of the code in other private functions like this
callMyOtherEncapsulatedFunction();
}

... Button2_click(...)
{
   // Do stuff only when the page posts back because button TWO was clicked. You can even call custom classes in a library file...

CustomClassInLibrary newClass = new CustomClassInLibrary();
newClass.callMyFunctionInTheCustomClass();
}

private ... callMyOtherEncapsulatedFunction()
{
   // Do lots of complicated stuff that you didn't want to have to code in the click event, or the you encapsulated for reuse, code clarity, or to provide better abstraction in your application. Yet, since only the Button1 click event calls this function, it ONLY happens when Button1 has been clicked.
}
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
The Following User Says Thank You to chroniclemaster1 For This Useful Post:
Gayathri79 (May 24th, 2010)
 
Old May 24th, 2010, 04:04 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

First a little disclaimer: I completely agree with previous posters in that you probably don't need to find out which button was clicked in Page_Load. Instead, you should probably restructure your code so the buttons can handle whatever they need to handle, and Page_Load sets the stage for the buttons to do what they need to do.

Also, the solution I am posting here has issues on its own. Server IDs may change based on their location in the page hierarchy, you may rename your buttons and so on. It also defeats the idea of object orientation and uses techniques that ASP.NET was designed for to minimize.

That all said, you can simply read the Request.Form collection to see what which button caused the post back. Using myButton.UniqueID you get the name attribute used in the client HTML:

Code:
 
if (!string.IsNullOrEmpty(Request.Form[Button1.UniqueID]))
{
  Label1.Text = "Button 1 clicked";
}
if (!string.IsNullOrEmpty(Request.Form[Button2.UniqueID]))
{
  Label1.Text = "Button 2 clicked";
}
But, like I said (and others before me): don't use this code. Find a better solution.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Gayathri79 (May 24th, 2010)
 
Old May 24th, 2010, 04:55 PM
Authorized User
 
Join Date: Mar 2010
Posts: 65
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Thanks. I'le try to restructure my code like you said.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to know whick button was clicked. KingArthur Struts 1 February 22nd, 2006 07:12 AM
Determining Which Button Was Clicked BrianWren ASP.NET 1.0 and 1.1 Basics 3 May 5th, 2005 03:24 PM
How to tell which button clicked emily PHP How-To 2 November 30th, 2004 01:35 PM
links to a page when a button is clicked hosefo81 HTML Code Clinic 4 December 7th, 2003 06:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.