Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Somebody, Anybody ... How do I do a postback from a textbox??!!

Status
Not open for further replies.

VBRookie

Programmer
May 29, 2001
331
US
I've been having the hardest time figuring this out. There doesn't seem to be much documentation as to how to do it on the web. Has anyone done it before?

I want to call a server-side function from the onKeyPress event of the textbox control.

Please help!

- VB Rookie
 
To quote Jack

Yee Gads Man!
What are you thinking here? Do you really want do a round trip to the server everytime some one presses a key in a text box!! I have already typed 154 chars! That's 154 trips to the server. You can do a post back when someone leaves the field by setting the autopostback to true.

What is it that you are doing that you need such crazy functionality? Maybe I can help you rethink the design. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Well actually what I need to do is submit the form and call a server side function when the user hits the enter key from the textbox. So I'm figuring that I need a client-side onKeyPress function to figure out when they hit the enter key and then from that function call the server side routine.

Can this be done?

In old ASP I would just put this in a separate form but you can only have one in ASP.NET is my understanding so I can't do that anymore.

Any ideas?

- VB Rookie
 
Enter key as in a submit button? Or anytime they leave that textbox?

If you want option 1 then simply add a web form button.
If you want option 2 then what I said above.

<quote>You can do a post back when someone leaves the field by setting the autopostback to true on that field/textbox.</quote> That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Well the thing is they may never leave the field. What we have on our site is a little search box on each page. The users go there and enter in a name or something, then they can either click on the search button on just hit the enter key to initiate the search.

If they click the search button all works wonderfully. However if they simply type in the information and then hit the enter key the page just blinks and the search logic is never initiated on the server.

So I don't know that the autopostback idea would work if it requires them to actually leave the field. Are there any other options?

- VB Rookie
 
The search button, What type of button is it?

I just tested it and when I hit enter the code for the first button in the html code is ran. Can you run the debugger and see where the code is jumping when you press enter? That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
It jumps to the page_load routine and stops.

What I tried to do was set a value in a hidden field when the enter key was pressed and they in the page_Load routine on the server check that value. If it was set to a certain value then I would call the search code. That didn't work out though because I kept getting a javascript error saying:

Microsoft JScript runtime error: 'document.frmMain.EnterHit' is null or not an object

When I did a view source I found that my control name had been changed at runtime to:

_ctl3:Search_form1:EnterHit

So my javascript function couldn't find it.
Any ideas? I feel that I'm at my wit's end here.

- VB Rookie
 
couple questions

Are you using spaggetti code or code behind?

Could I see your code? It may just be something stupidly simple. If the code is rather large you can email me.

mark at shertek.com

Tip: exchange at for @ I am wary of email spiders That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
No I'm using a code behind. The files aren't that big. Here is the aspx (or rather ascx since its a user control):

Code:
<%@ Control Language=&quot;c#&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;search_form.ascx.cs&quot; Inherits=&quot;Sbc.UI.Web.UserControls.search_form&quot; TargetSchema=&quot;[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5&quot;[/URL] %>
<!-- SEARCH FORM START -->

<asp:table id=&quot;TABLE1&quot; Runat=&quot;server&quot; BackImageUrl=&quot;../images/bg_search_area.jpg&quot; CELLPADDING=&quot;0&quot; CELLSPACING=&quot;0&quot; BorderWidth=&quot;0&quot; WIDTH=&quot;165&quot;>
	<asp:TableRow>
		<asp:TableCell>
			<IMG SRC=&quot;./images/hdr_s_a_employee_top.jpg&quot; WIDTH=&quot;165&quot; HEIGHT=&quot;22&quot; ALT=&quot;Enter either an SSN, a Last Name, or a last and first name in the format:  Last Name, First Name&quot;>
		</asp:TableCell>
	</asp:TableRow>
	<asp:TableRow>
		<asp:TableCell HorizontalAlign=&quot;Center&quot;>
			<input type=&quot;text&quot; id=&quot;txtEmployee&quot; CLASS=&quot;style_s_01&quot; size=&quot;20&quot; value=&quot;Enter Last Name or SSN&quot; onfocus=&quot;this.value='';&quot; runat=&quot;server&quot; />
		</asp:TableCell>
	</asp:TableRow>
	<asp:TableRow>
		<asp:TableCell>
			<asp:Image ImageUrl=&quot;./images/spacer.gif&quot; Width=&quot;1&quot; Height=&quot;7&quot; Runat=&quot;server&quot; />
		</asp:TableCell>
	</asp:TableRow>
	<asp:TableRow>
		<asp:TableCell HorizontalAlign=&quot;Center&quot;>
			<asp:ImageButton id=&quot;btnSearch&quot; ImageUrl=&quot;../images/btn_select.gif&quot; WIDTH=&quot;59&quot; HEIGHT=&quot;19&quot; ALT=&quot;Select an Employee&quot; OnClick=&quot;Perform_Search&quot; Runat=&quot;server&quot; />
		</asp:TableCell>
	</asp:TableRow>
	<asp:TableRow>
		<asp:TableCell>
			<asp:Image id=&quot;imgBottom&quot; ImageUrl=&quot;../images/hdr_s_a_employee_btm.jpg&quot; WIDTH=&quot;165&quot; HEIGHT=&quot;13&quot; ALT=&quot;Select an Empolyee&quot; Runat=&quot;server&quot; />
		</asp:TableCell>
	</asp:TableRow>
</asp:table><input id=&quot;EnterHit&quot; type=&quot;hidden&quot; value=&quot;N&quot; runat=&quot;server&quot;>

<!-- SEARCH FORM END -->

Here is the code behind file:

Code:
namespace Sbc.UI.Web.UserControls
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using Sbc.UI.Web.Utilities;
	
	/// <summary>
	///		Summary description for search_form.
	/// </summary>
	public class search_form : BaseControl
	{
		protected System.Web.UI.WebControls.ImageButton btnSearch;
		protected System.Web.UI.HtmlControls.HtmlInputText txtEmployee;
		protected System.Web.UI.WebControls.Table TABLE1;
		protected System.Web.UI.WebControls.TextBox TextBox1;
		protected System.Web.UI.HtmlControls.HtmlInputHidden EnterHit;

		private void Page_Load(object sender, System.EventArgs e)
		{
			
			System.Text.StringBuilder jScriptStr = new System.Text.StringBuilder();
			jScriptStr.Append(&quot;<script language=javascript>&quot;);
			jScriptStr.Append(&quot;function FindKey(){&quot;);
			jScriptStr.Append(&quot;if (event.keyCode == 13) {&quot;);
			jScriptStr.Append(&quot;document.frmMain.EnterHit.value='Y';document.frmMain.submit();}}&quot;);
			jScriptStr.Append(&quot;</script>&quot;);
			Page.RegisterStartupScript(&quot;cs&quot;, jScriptStr.ToString());
			txtEmployee.Attributes.Add(&quot;onKeyPress&quot;,&quot;FindKey();&quot;);
			
			if (EnterHit.Value == &quot;Y&quot;)
			{
				Session[ConstantValues.SessionVarNewSearch] = &quot;T&quot;;
				Server.Transfer(&quot;selectEmployee.aspx?txtEmployee=&quot; + txtEmployee.Value + &quot;&NewSearch=T&quot;);
			}
			
		}

		public void Perform_Search(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			Session[ConstantValues.SessionVarNewSearch] = &quot;T&quot;;
			Server.Transfer(&quot;selectEmployee.aspx?txtEmployee=&quot; + txtEmployee.Value + &quot;&NewSearch=T&quot;);

		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

	}
}

Make any sense? I'm sure its just something really small and simple. I guess that I've been looking at it so long I can't tell anymore.

- VB Rookie
 
doh!

I didn't know you were using a usercontrol. I am not sure how to do this with a user control but if I run across something I'll be sure to post it. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
WoooooooHoooooo!!!!

I've got it.
It's just a matter of a simple change in my javascript code.
UserControls use the INamingContainer.

So to reference the name of the control without getting a Javascript error, I have to do something like this:

document.forms['frmMain']['&quot; + EnterHit.UniqueID + &quot;'].value='Y'

UniqueID will give me the ID that the control will be changed to in the HTML. I changed my code to use this syntax and it works beautifully. Many thanks for your help man.

- VB Rookie
 
Glad you got it figured out. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top