Which control has performed (triggered) PostBack event?

J

John Smith

How can I find out which control performed PostBack?

I have put this code inside my Page_Load event:

Response.Write(Page.Request.Params.Get("__EVENTTARGET"))

but it doesn't write to response anything when I click on the button inside
DataGrid (my asp:Button is located inside my template column togrther with
my asp:TextBox). It works fine for DropDownList, which is also inside
template column inside DataGrid.
 
K

Karl Seguin [MVP]

Request.Form["__EVENTTARGET"] will only be available when a control other
than a button caused postback. That's because <asp:button's> generate a
postback naturally in HTML without the help of javascript. LinkButton's and
DropDownList's for example require JavaScript to do a postback, and thus
write into __EVENTARG.

Unfortunetly, the only way to determine which button causes a postback
(without waiting for the event handler to fire), is to loop through
Request.Form and look for the button's UniqueID. Which ever button causes
the postback will be in the Request.Form collection - all other button's
won't be.

Karl
 
M

Marina Levit [MVP]

In addition to what Karl said, typically, you shouldn't need to do this. You
handle the appropriate event for each control, and it will get called if
necessary. So if you clicked button A, button A's click handler will be
called without you having to do anything. If it was button B, then button
B's click handler will get called. You shouldn't need to manually figure
this out unless you are doing something unusual.
 
K

Karl Seguin [MVP]

I agree, though I've had some cases where I've had to look right into
Request.Form. It always feels uncomfortable, but it might be necessary to
shortcircuit heavy processing in some cases. If at all possible, such code
should be pushed to the PreRender event, which will happen after the
eventHandler fires, but that's also not always possible.

I think to recap...make sure you actually have to do this before doing it :)

Karl
 
J

John Smith

Request.Form["__EVENTTARGET"] will only be available when a control other
than a button caused postback. That's because <asp:button's> generate a
postback naturally in HTML without the help of javascript. LinkButton's
and DropDownList's for example require JavaScript to do a postback, and
thus write into __EVENTARG.

Your explanation helped me, but I still haven't solved my problem entirely.
When my asp:TextBox have focus then my asp:Button changed its color,
indicating that it will submit form on ENTER keyboard button pressed. I do
have the OnClick event associated with this button, but it doesn't execute
when I press ENTER inside my asp:TextBox, because this button is not inside
Request.Form array. The button is not inside Request.Form array of
components because (I think) ASP.Net event was not fired, but natural HTML
event was fired. When I click my asp:Button with mouse pointer, the OnClick
event fire.

What should I do? :(
 
K

Karl Seguin [MVP]

Well, in ASP.NET 1.1, hitting "enter" in a textbox will cause the first
button in the form to submit. This is an HTML behaviour. It should be
causing the event handler to fire though - that's strange. In asp.net 2.0,
you can control which button is fired when enter is clicked in each textbox.
My guess is that this is done via javascript and __EVENTTARGET would be
used.

If you want finer control over enter in textboxes in 1.x, check out:
http://www.allasp.net/enterkey.aspx

it might solve ur problem too..

Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/


John Smith said:
Request.Form["__EVENTTARGET"] will only be available when a control
other
than a button caused postback. That's because <asp:button's> generate a
postback naturally in HTML without the help of javascript. LinkButton's
and DropDownList's for example require JavaScript to do a postback, and
thus write into __EVENTARG.

Your explanation helped me, but I still haven't solved my problem
entirely. When my asp:TextBox have focus then my asp:Button changed its
color, indicating that it will submit form on ENTER keyboard button
pressed. I do have the OnClick event associated with this button, but it
doesn't execute when I press ENTER inside my asp:TextBox, because this
button is not inside Request.Form array. The button is not inside
Request.Form array of components because (I think) ASP.Net event was not
fired, but natural HTML event was fired. When I click my asp:Button with
mouse pointer, the OnClick event fire.

What should I do? :(
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top