Determining if a textbox control caused a postback

Discussion in 'ASP .Net' started by RTM, Jan 16, 2004.

  1. RTM

    RTM Guest

    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.
    RTM, Jan 16, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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.
    Steve C. Orr [MVP, MCSD], Jan 16, 2004
    #2
    1. Advertising

  3. RTM

    mikeb Guest

    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
    mikeb, Jan 16, 2004
    #3
  4. RTM

    RTM Guest

    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.
    RTM, Jan 20, 2004
    #4
  5. RTM

    mikeb Guest

    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
    mikeb, Jan 20, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski

    Determining What Control Caused The PostBack

    Nathan Sokalski, Sep 5, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    926
    Nathan Sokalski
    Sep 5, 2005
  2. =?Utf-8?B?ZGNoaWxsbWFu?=

    Determining control that caused a postback

    =?Utf-8?B?ZGNoaWxsbWFu?=, Oct 18, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    4,334
    =?Utf-8?B?ZGNoaWxsbWFu?=
    Oct 19, 2005
  3. Nathan Sokalski

    Determining What Control Caused The PostBack

    Nathan Sokalski, Sep 5, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    4
    Views:
    127
    Nathan Sokalski
    Sep 5, 2005
  4. Nathan Sokalski

    Determining What Control Caused The PostBack

    Nathan Sokalski, Sep 5, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    148
    Nathan Sokalski
    Sep 5, 2005
  5. matthias s

    determining what control caused the click event

    matthias s, Sep 19, 2007, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    173
    David R. Longnecker
    Sep 20, 2007
Loading...

Share This Page