Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Determining if a textbox control caused a postback

Reply
Thread Tools

Determining if a textbox control caused a postback

 
 
RTM
Guest
Posts: n/a
 
      01-16-2004
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.
 
Reply With Quote
 
 
 
 
Steve C. Orr [MVP, MCSD]
Guest
Posts: n/a
 
      01-16-2004
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.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net


"RTM" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.



 
Reply With Quote
 
 
 
 
mikeb
Guest
Posts: n/a
 
      01-16-2004
RTM wrote:
> 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.

--
mikeb
 
Reply With Quote
 
RTM
Guest
Posts: n/a
 
      01-20-2004
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.
 
Reply With Quote
 
mikeb
Guest
Posts: n/a
 
      01-20-2004
RTM wrote:
> 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.

--
mikeb
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
determining what control caused the click event matthias s ASP .Net Web Controls 5 09-20-2007 12:11 PM
Determining control that caused a postback =?Utf-8?B?ZGNoaWxsbWFu?= ASP .Net 4 10-19-2005 12:28 PM
Determining What Control Caused The PostBack Nathan Sokalski ASP .Net Web Controls 4 09-05-2005 10:42 PM
Determining What Control Caused The PostBack Nathan Sokalski ASP .Net Datagrid Control 4 09-05-2005 10:42 PM
Determining What Control Caused The PostBack Nathan Sokalski ASP .Net 5 09-05-2005 10:42 PM



Advertisments