p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/forumdisplay.php?f=351)
-   -   How to check if Button is Clicked in ASP? (http://p2p.wrox.com/showthread.php?t=79502)

Gayathri79 May 21st, 2010 02:58 PM

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?

samjudson May 21st, 2010 04:02 PM

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?

Gayathri79 May 22nd, 2010 03:29 PM

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.

chroniclemaster1 May 23rd, 2010 02:33 PM

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.
}


Imar May 24th, 2010 04:04 AM

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

Gayathri79 May 24th, 2010 04:55 PM

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


All times are GMT -4. The time now is 03:39 AM.

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