in page_load detect what button was clicked

  • Thread starter thomasamillergoogle
  • Start date
T

thomasamillergoogle

I am trying to detect what asp.net button was clicked duing the
page_load event. I have two asp.net buttons on my page.

What is the easiest way to detect this?
 
K

Kevin Spencer

Is there some overarching reason that you picked the Page_Load event? How
about using an Event Handler?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
K

Karl Seguin

There is no easy way.

If they are link buttons, you can look at Request.Form["_eventtarget"];

if they are buttons, if the id of the button is in request.form, then that's
the button that was clicked..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
J

Josh

I appreciate (sorry ) that I'm not answering your question, but normal
process would be to handle that in the buttons click events. You may be
heading the wrong way if your trying to work around that.

What are you trying to achieve?
 
K

Karl Seguin

I've come across situations where I've cheated on this one also. Typically
it's performance related (not a mindless micro-optimisation either)...

But I agree that it should be called into question and thoroughly examined
:)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
T

thomasamillergoogle

Yes, of course I would usually handle this in the normal event handler.
But for my case I want to do something like this in my page_load

if(page.ispostback)
{
if (button1 was clicked)
{
//do something
}
else
{
//do some normal postack type stuff
}
}
 
S

sam

You can also always render a hidden field in your page and set that
value = to a unique button string identifier in the onlick javascript
for the two buttons. Then on page load you could just check that
hidden field value. Make sure you either declare that hidden field in
the web page or add it in the Init() method, though, otherwise it won't
participant in the load post data phase.

ASP.NET is kindof retarded about this. You would think they would
expose an easy way to let us get this kindof information in the page
load.

-Sam
 
G

gabe garza

C#
if(null != Request.Form["buttonName"])
{
// buttonName was clicked
}

VB.NET
If Not Request.Form("buttonName") Is Nothing Then
' buttonName was clicked
End If
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top