How To Determine What Caused PostBack

J

Jordan

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!
 
K

Kevin Spencer

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

Jordan

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

: )
 

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,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top