469,926 Members | 1,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

How To Determine What Caused PostBack

I am dynamically inserting an html <input> tag as text (equivalent of an
image button) into a page via a Literal control.

Something like this gets inserted:
<input type="image" name="MyImageButton" id="MyImageButton"
src="somePic.jpg" />

I want for the code-behind to "know" when the user clicked on the image -
but I don't want to also have to dynamically specify a delegate for an event
procedure to handle this control which is really inserted as a simple string
at runtime under limited conditions.

I was thinking/hoping that I could simply determine (somehow) in the
Page_Load event if the current PostBack was caused by a user clicking on the
image - and then take required action. So, how can I determine in Page_Load
that the current PostBack was caused by the user clicking on the
image/<input>?

Thanks!
Nov 19 '05 #1
3 2054
An HTML image input element does the same thing that an HTML submit input
element does. It submits the form. When any form element or JavaScript that
submits a form submits the form, the form doesn't contain any information
regarding what element submitted the form. Now, there are a couple of ways
to pass this information back to the server, and both of them require
JavaScript. One (the easiest) is to use a Server Control for your HTML image
input element, and to write a server-side Event Handler for it. However, you
mysteriously stated that you "don't want to also have to dynamically specify
a delegate for an event procedure to handle this control..." I find it
mysterious because you didn't state any logical justification for not
wanting to do that. It almost appears as if you have something against the
ASP.Net object-oriented programming model. The second way is to roll your
own event handler, by adding an "onclick" client-side JavaScript event
handler that puts the ID or name of the control into a hidden form field and
submits the form. On the server side, the class can then read the contents
of the hidden form field to find out what element submitted the form.
Interestingly enough, this is exactly what ASP.Net does when you use a
Server Control with an Event Handler delegate.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

"Jordan" <A@B.com> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
I am dynamically inserting an html <input> tag as text (equivalent of an
image button) into a page via a Literal control.

Something like this gets inserted:
<input type="image" name="MyImageButton" id="MyImageButton"
src="somePic.jpg" />

I want for the code-behind to "know" when the user clicked on the image -
but I don't want to also have to dynamically specify a delegate for an
event procedure to handle this control which is really inserted as a
simple string at runtime under limited conditions.

I was thinking/hoping that I could simply determine (somehow) in the
Page_Load event if the current PostBack was caused by a user clicking on
the image - and then take required action. So, how can I determine in
Page_Load that the current PostBack was caused by the user clicking on the
image/<input>?

Thanks!

Nov 19 '05 #2
<< you didn't state any logical justification for not wanting to do that>>

I'm lazy! Is that logical enough?! I was just hoping for some easier way
and I'm relatively new to Web programming in general so the specifics of how
the submits take place - as you explained - weren't understood. For all I
knew the "thing that caused the submit" was passed as a form variable or
parameter as part of the normal course of operations on the Web.

I sure appreciate your explanation and perspective - very helpful from you
as usual.

It looks like I'll have to do some work here - darn it.

: )
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
An HTML image input element does the same thing that an HTML submit input
element does. It submits the form. When any form element or JavaScript
that submits a form submits the form, the form doesn't contain any
information regarding what element submitted the form. Now, there are a
couple of ways to pass this information back to the server, and both of
them require JavaScript. One (the easiest) is to use a Server Control for
your HTML image input element, and to write a server-side Event Handler
for it. However, you mysteriously stated that you "don't want to also have
to dynamically specify a delegate for an event procedure to handle this
control..." I find it mysterious because you didn't state any logical
justification for not wanting to do that. It almost appears as if you have
something against the ASP.Net object-oriented programming model. The
second way is to roll your own event handler, by adding an "onclick"
client-side JavaScript event handler that puts the ID or name of the
control into a hidden form field and submits the form. On the server side,
the class can then read the contents of the hidden form field to find out
what element submitted the form. Interestingly enough, this is exactly
what ASP.Net does when you use a Server Control with an Event Handler
delegate.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Neither a follower nor a lender be.

"Jordan" <A@B.com> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
I am dynamically inserting an html <input> tag as text (equivalent of an
image button) into a page via a Literal control.

Something like this gets inserted:
<input type="image" name="MyImageButton" id="MyImageButton"
src="somePic.jpg" />

I want for the code-behind to "know" when the user clicked on the image -
but I don't want to also have to dynamically specify a delegate for an
event procedure to handle this control which is really inserted as a
simple string at runtime under limited conditions.

I was thinking/hoping that I could simply determine (somehow) in the
Page_Load event if the current PostBack was caused by a user clicking on
the image - and then take required action. So, how can I determine in
Page_Load that the current PostBack was caused by the user clicking on
the image/<input>?

Thanks!


Nov 19 '05 #3
Jordan,

Arne wrote up an innovative article on how to wire events with
reflection: http://www.jroller.com/page/CoBraLorD/20040811
which illustrates how to do it without adding a delegate explicitly. It
should be fairly easy to adapt the code to your
needs

-Stefan

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Joe | last post: by
5 posts views Thread by Joey G | last post: by
2 posts views Thread by news.microsoft.com | last post: by
reply views Thread by John A Grandy | last post: by
14 posts views Thread by Mike Collins | last post: by
reply views Thread by =?Utf-8?B?YmVycnkubW9yZ2Fu?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.