473,382 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Determining if a textbox control caused a postback

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.
Nov 18 '05 #1
4 2131
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" <ry***********@mindspring.com> wrote in message
news:5e**************************@posting.google.c 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.

Nov 18 '05 #2
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
Nov 18 '05 #3
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.
Nov 18 '05 #4
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
Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Martin | last post by:
Hi, I have created a composite control that has a number of standard asp.net controls on it that can themselves cause postbacks. What i need to do in my composite control is to determine which...
11
by: Joe | last post by:
Hello All, I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton (SearchButton) server controls. The user can type search text in SearchTextBox and click SearchButton and...
2
by: fabrice | last post by:
Hi, I have a trouble with my TextBox Control and it's ViewState in my form... The property for it, is FALSE as following : <form id="frmSearchBre4" runat="Server"> <asp:textbox id="myID"...
1
by: Opa | last post by:
Hi, In my page load event I set the Text value of a TextBox control as follows: private void Page_Load(object sender, System.EventArgs e) { this.txtName = "Joe" } I have a button which...
5
by: Nathan Sokalski | last post by:
I have a DataList that I was having trouble getting the events for. After a bit of help, I realized that I needed to put the databinding inside an If Not IsPostBack() condition. Although this fixed...
3
by: Mad Bull | last post by:
I am a newbie. I have created a page which upon load connects to SQL Server and runs a stored procedure. The data returned from this is assigned to some asp:labels and asp:textbox controls. This...
4
by: PokerMan | last post by:
Hi I have a few controls on apage that cause a postback. But want to handle a postback differently depending on which one of these controls fired the postback. How do we do this? c#. Thanks
6
by: chenhong | last post by:
I have a aspx page which has a textbox control and a button control. I added some code in the button's OnClick event. When I add some text in the textbox and hit ENTER,the page postback and the...
6
by: Shelly | last post by:
In looking at the asp.net TextBox, I noticed that there is a property called "OnTextChanged". I tried to use this control to call a subroutine called "GetCompanyProperties". The field is the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.