Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7792987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:20:41+00:00 2026-06-01T22:20:41+00:00

The problem I have when the following code executes, it will first display the

  • 0

The problem I have when the following code executes, it will first display the original contents of the textbox, then each set of keys that I pressed up until hitting ‘Enter’. So if the textbox originally contained ‘Ice Cream – Pierre’s Chocolate Chip’ and I click in the textbox to get focus and replace it with ‘Tea’, I will get 4 alert boxes, each with the following content:

  1. Ice Cream – Pierre’s Chocolate Chip
  2. T
  3. Te
  4. Tea

And then if I click on a different row and do something else, I will get the original 4 alert boxes from before and then the new ones…it keeps stacking and I don’t know why?

Here is some code from my .cs file – note the “OnKeyPress” attribute I added:

        public TCell(String celltext,String strTextBox,String strName)
        {
            int iWidth = 0;
            int intRandom =
            iWidth = Convert.ToInt32(strTextBox);
            tblCell = new TableCell();
            TextBox txtItem = new TextBox();
            txtItem.ID = strName;
            txtItem.Text = celltext;
            txtItem.Width = iWidth;
            txtItem.BorderStyle = BorderStyle.None;
            txtItem.CssClass = "cssAdjust trans";
            txtItem.Style.Add("padding", "0px 5px 0px 5px");
            txtItem.Attributes.Add("onKeyPress","captureTable(this.value);");
            txtItem.Attributes.Add("onFocus", "javascript:this.select();");
            tblCell.Controls.Add(txtItem);
        }

And then here is the function where I am displaying once the enter key is pressed:

 function captureTable(strValue) 
 {
     $(document).keypress(function (e) {
         if (e.which == 13) // Enter has been pressed.
         {
             e.preventDefault();
             document.getElementById("<%=HiddenField1.ClientID %>").value = strValue;
             alert(strValue);
         }
     });

Thanks in advance for any help you can give me. I had this working using “window.event” but that locks me into IE from what I read and I am trying to use JQuery for this.

(UPDATE):

Ok, I used the code provided by kmb385 below, but I think I’m missing something still. I don’t get any alerts appearing, so I set a breakpoint at the ‘$(“.txtHook”).keypress(function(e){‘ line and it doesn’t reach it during key entry inside of the table text box, including the enter key.

Here is where the code resides within the default.apsx page:

 <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

  <script type="text/javascript">

  $(document).ready(function(){ 
    $(".txtHook").keypress(function(e){ 
         if (e.which == 13) // Enter has been pressed.
         {
             e.preventDefault();
             document.getElementById("<%=HiddenField1.ClientID %>").value = $(this).val(); 
             alert(strValue);
         }
     });
  });

My inclusion of JQuery is within the body tag of the site’s master page:

<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
            <Scripts>
                <asp:ScriptReference Path="~/Scripts/jquery-1.4.1.min.js" />
            </Scripts>
        </asp:ScriptManager>

(FINAL UPDATE:)

Turns out my inclusion of JQuery was incorrect. I moved it to the head tag of the master page and changed the code to read:

<script type="text/javascript" src="../scripts/jquery-1.4.1.min.js"></script>

After that, the code provided my kmb385 started to work 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:20:43+00:00Added an answer on June 1, 2026 at 10:20 pm

    Your original javascript code was binding an anonymous function to the keypress event on the document, each time a key was pressed in an input you created. This resulted in multiple functions being fired each time a key was pressed anywhere on the document/page. I removed your binding to the onKeyPress event in the .cs file. Also in the cs file I added a class attribute to the textbox so I could hook into it in jquery.

    In the Javascript I use the class selector in jquery to bind a function to the keypress event of each input. The function gets the value of the input and assigns it to the hidden input. There may be issues with this code because I’m not familiar with .net, if there is let me know and we can work through them. I am skeptical of the following selector but I’m guessing its a .net thing.

    document.getElementById(“<%=HiddenField1.ClientID %>”).value

    public TCell(String celltext,String strTextBox,String strName)
    {
        int iWidth = 0;
        int intRandom =
        iWidth = Convert.ToInt32(strTextBox);
        tblCell = new TableCell();
        TextBox txtItem = new TextBox();
        txtItem.ID = strName;
        txtItem.Text = celltext;
        txtItem.Width = iWidth;
        txtItem.BorderStyle = BorderStyle.None;
        txtItem.CssClass = "txtHook cssAdjust trans";
        txtItem.Style.Add("padding", "0px 5px 0px 5px");
        txtItem.Attributes.Add("onFocus", "javascript:this.select();");
        tblCell.Controls.Add(txtItem);
    }
    

    Javascript

        $(document).ready(function(){
            $(".txtHook").keypress(function(e){
                if (e.which == 13) // Enter has been pressed.
                {
                    e.preventDefault();
                    document.getElementById("<%=HiddenField1.ClientID %>").value = $(this).val();
                    alert(strValue);
                }
            });
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, and i have a problem regarding changing the ringtone
I have the following code: The actual problem is the non-quoted code. I want
I have the following code I want to run, but the problem is $this->type
The following code summarizes the problem I have at the moment. My current execution
I have the following code, but i got a problem. Within my While, a
I have the following code: #include <iostream> #include Student.h <-- fixed the problem #include
I am currently working on Problem 62 I have tried the following code to
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
I have a problem with the following code: protected void onDraw(Canvas canvas) { Paint
I have problems with following code: http://lisper.ru/apps/format/96 The problem is in normalize function, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.