Results 1 to 5 of 5

Thread: asp.net 2003 ENTER key press in Multiple TextBoxes for multiple events

  1. #1
    Join Date
    Mar 2006
    Posts
    35
    Rep Power
    0

    Default asp.net 2003 ENTER key press in Multiple TextBoxes for multiple events

    hello,

    I'm trying to trap for the ENTER key in 3 text boxes, each textbox ENTER key press will call a different sub routine.
    ex.

    TextBox1 - should call sub routine - DoThis1
    TextBox2 - should call sub routine - DoThis2
    TextBox3 - should call sub routine - DoThis3

    I've found a lot of javascript examples that should work for this, however, upon execution TextBox1 ENTER key press works as it should, but TextBox2 and TextBox3 Enter key press call the first button click event on the page

    Also, the asp textboxes are not allowing an onkeypress option in the html code.

    Does anyone have any suggestions? Anything would be helpful.

    thank you

  2. #2
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I am guessing that you might be able to test if the control has focus and keydown before trapping the key?

    I have some html javascipt code on this but I wont be able to get them of my machine until week end
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  3. #3
    Join Date
    Mar 2006
    Posts
    35
    Rep Power
    0

    Default

    thank you crosswire, I'm more than willing to wait for what you have

  4. #4
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Oh the problem is with how the asp page wires up with the functions that you have. There is a "__doPostBack()" javascript function which is created in the "generated" web page from the ASP code.

    I can help you to wireup the page correctly, but take a look at this code. It was done in VS2005. My apologizes ... VS2002 needs a little reinstalling and lazy to fix that today.

    Default.aspx
    Code:
    <%@ Page Language="C#" AutoEventWireup="false"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        
        <script language="javascript" id="trapEnterScript" type="text/javascript">
        function trapEnter()
        {
            //IE 6 code
            window.status= "Key pressed has code = '" + event.keyCode + "'";
            if(event.keyCode == 32)//space key
            {
                //call do post back
                __doPostBack('doNothing','');
            }
            else
            {
                event.returnValue = false;//use up the event, rather cancel the event, so that no key is written in the text box
            }
            //IE 6 code ends
        }
        </script>
        
        <div>
            <input id="Button1" type="button" value="button" onserverclick="Button1_ServerClick" runat="server" /><br />
            <input id="Text1" type="text" onkeydown="trapEnter();" />
            <input id="Text2" type="text" onkeydown="trapEnter();" />
            <input id="Text3" type="text" onkeydown="trapEnter();" /><br />
            <input id="doNothing" type="button" value="Do nothing" onclick="return;" onserverclick="DoNothing_ServerClick" runat="server" /></div>
        </form>
    </body>
    
    </html>
    Default.aspx.cs
    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void Button1_ServerClick(object sender, EventArgs e)
        {
            Page.Response.Write("This is a post back<br>");
        }
        protected void DoNothing_ServerClick(object sender, EventArgs e)
        {
            //doNothing 'onclick' has a 'return' javascript code which blocks the following postback call of these functions. See DoNothing attributes
            DoThis1();
            DoThis2();
            DoThis3();
        }
        protected void DoThis1()
        {
            Page.Response.Write("This 1 was done<br>");
        }
        protected void DoThis2()
        {
            Page.Response.Write("This 2 was done<br>");
        }
        protected void DoThis3()
        {
            Page.Response.Write("This 3 was done<br>");
        }
    }
    Notice that I called "__doPostBack('doNothing','');" to a control with id doNothing. This basically sends or submitts the form, but the parameter, or control id, is used by the server to start a server event handle which needs an object sender and argument ie EventHandler(Object, Args). The easiest way to wire up a control id to a server event handle is to create a dummy server control and 'double click' it and it wires up atomatically, and the you manually uses that dummy control id in the "__doPostBack" function and summon the sever event handler. Note too I used html controls and not server controls.

    I messed up the trap code for the enter key. I said before that the focus should be testes but the event property must be called for IE, and something different for other browsers... I realized too that event is not present in HTML 4.01, therefore key trapping would take at least too versions of coding to get it compatible with all browsers.

    Anyway, post a snippet if you need help
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  5. #5
    Join Date
    Mar 2006
    Posts
    35
    Rep Power
    0

    Default

    Thanks Crosswire, I'll checkthis out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •