Determining if a textbox control caused a postback

R

RTM

Can anyone help me with the following issue? I've seen some similar
questions here, but none relating to a textbox control....

I have a form with several controls, one of them being a textbox
control with AutoPostBack set to true. I need to fire a method ONLY
when the form has posted back due to a button click (no problem here),
or from the user typing text in the text box and hitting enter. I
can't call this method from any postback, as I have other controls
that can cause postbacks as well.

So, in my code behind page I need to determine if the postback was
fired by the user hitting enter from the textbox. I'm at the end of
my ideas here - can anyone lend a hand?

Thanks.
 
S

Steve C. Orr [MVP, MCSD]

You could use client side script to intercept the keypress event of the
textbox, and if it's and enter key then set a value in a hidden field.
From your code behind you then check the value of the hidden field to see if
the enter key was pressed in the textbox or not.
 
M

mikeb

RTM said:
Can anyone help me with the following issue? I've seen some similar
questions here, but none relating to a textbox control....

I have a form with several controls, one of them being a textbox
control with AutoPostBack set to true. I need to fire a method ONLY
when the form has posted back due to a button click (no problem here),
or from the user typing text in the text box and hitting enter. I
can't call this method from any postback, as I have other controls
that can cause postbacks as well.

So, in my code behind page I need to determine if the postback was
fired by the user hitting enter from the textbox. I'm at the end of
my ideas here - can anyone lend a hand?

Thanks.

Request.Form["__EVENTTARGET"] should have the ID of your textbox
control in this case (assuming the postback method is POST not GET).

Note that this is undocumented, unsupported, subject to change, etc.

If you're concerned about this, you can view source in the browser to
see how they're doing it, and add JavaScript yourself to perform a
similar action that's entirely under our control.
 
R

RTM

Thanks to both of you for your responses. I've discovered a new
catch, however... It appears that hitting the enter key from anywhere
within the form will cause a postback - focus does not have to be set
within the textbox in question. So, I need to revise this question to
see how I can determine if the enter key (regardless of focus) caused
a postback. I tried to get the Request.Form["__EVENTTARGET"] value,
but it is coming up empty when called from my codebehind page. Any
ideas?

Thanks.
 
M

mikeb

RTM said:
Thanks to both of you for your responses. I've discovered a new
catch, however... It appears that hitting the enter key from anywhere
within the form will cause a postback - focus does not have to be set
within the textbox in question. So, I need to revise this question to
see how I can determine if the enter key (regardless of focus) caused
a postback. I tried to get the Request.Form["__EVENTTARGET"] value,
but it is coming up empty when called from my codebehind page. Any
ideas?

Thanks.

The __EVENTTARGET field only gets set to the textbox ID if the textbox
is set to autopostback and is changed.

Since you need finer control over identifying the textbox at a
particular time, you should take Steve Orr's advice and emit your own
JavaScript keypress handler for the textbox, and store information in a
hidden form field if the keypress is enter.

You can study the __doPostBack() handler that sets the __EVENTTARGET
field in the HTML that ASP.NET emits for an idea of how this is done.
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top