473,396 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Responding to Command button clicks in Page_Load

I have quite a few pages that have multiple command buttons on them - for
example one button might be to logout, and others might be to display
different segments of the data being shown. The important point is that
what gets displayed in the page depends on which command button was clicked
in the postback. I'd assumed that I could handle this by sorting out what
action to take in the Click event handlers for the buttons, but that doesn't
work because the event isn't raised until after Page_Load has completed and
the HTML output has presumably already been determined.

So if I can't use the Click event, how do I determine during my Page_Load
code which button has been clicked and therefore what output to display?

Thanks

Paul

Nov 18 '05 #1
3 1550
Hi Paul,

In the Form_Load, you can check the string:

Request.Form.ToString ()

The last item in the string should be the name of button you click.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #2
Interesting. A very convenient workaround. I tried that and it works for
Button controls but not for LinkButton controls (which is what I would have
preferred to have though I guess it's not essential). If you hit a
LinkButton then the identity of the LinkButton doesn't seem to get added to
the Form data. Which raises two additional questions:
1. Is there any similar workaround for LinkButtons.
2. Is this an 'approved' workaround or is it a hack that happens to work at
the moment but could easily get broken by a future .NET release?

So far the only alternative I've come up with is to manually add some
client-side Javascript to the buttons which sets the value of a hidden
control when each Button (or LinkButton) is clicked. Then back on the server
I can read the value of this hidden control to figure out what button was
clicked. AFAICS that should work, but it looks messy so I don't really want
to do that. (Plus it seems to be exactly the kind of manual boilerplate
coding for managing the controls that I thought ASP.NET was supposed to
release us from).

Paul
"MSFT" <lu******@online.microsoft.com> wrote in message
news:Q8**************@cpmsftngxa07.phx.gbl...
Hi Paul,

In the Form_Load, you can check the string:

Request.Form.ToString ()

The last item in the string should be the name of button you click.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #3
Hi Paul,

I just found a better solution which also work for a linkbutton:

Page.Request.Form["__EVENTTARGET"]

Every Form in ASP.NET have a hidden filed "__EVENTTARGET", we can get the
event sender form this field.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael Johnson Jr. | last post by:
I am trying to handle a button click event, which updates a web control table with data. The button is dynamically created in the table itself. When I call updateTable() in the Page_Load the new...
4
by: John Smith Jr. | last post by:
I have a very strange problem, I am dynamically creating buttons and events, when page loads the first click works fine, every click there after the event only fires every other click. I made a...
1
by: Milan Todorovic | last post by:
I need some help with my ASP.NET application. The central part of the application is very processing intensive page that can take up to several minutes to process the complex queries and load the...
1
by: Klom Dark | last post by:
I've got a weird problem going on - I've got a table of dynamically created buttons. Each button has the X/Y value of the buttons position in the table assigned to it's CommandArgument property and...
2
by: McGeeky | last post by:
I use page_load to call a stored procedure and populate the form. When the user clicks the OK button on the form I have implemented an event handler behind it (OKButton_Click()) that calls another...
7
by: Jimmy Jazz | last post by:
Hi, I have created a control with some buttons an alphabet. When one of these buttons are pressed it posts back and in the postback event for the button pressed a list of tems is created with a...
2
by: Radu | last post by:
Hi. I have a page with the following html: __________________________________________________________ <h1>Dynamic Controls Test</h1> <hr /> <asp:TextBox ID="txtProduct1" Text="txtProduct 1"...
2
by: CJM | last post by:
I have a page that allows the user to search a DB by querying one of 3 fields. When results are returned, I want the user to be able to click a value in one of three columns (that directly relate...
9
by: larryimic | last post by:
I have created a Access production database that records good parts and bad parts to a table thru querys using macros and command buttons on a form. A report (part label) is printed each time a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.