Need Help with Textbox event firing off.  Urgent

Posted on 2006-06-02
Last Modified: 2008-03-03
Hello all.  I need some help.  I have an enter key javascript event that will fire off the code I need.  I need this to fire off on a textbox.  Well I have one big problem.  The code in the ASPX is creating this textbox I need to fire off and I the onkeypress would fire.  I am trying to work around having to do a rebuild of the whole site code and just an ASPX change.  Here is the javascript:

<script language="javascript">

      function enterSubmits(eventObj, obj)
      alert('here ye');
            var keyCode
            // Check For Browser Type
            if (document.all){
            var str=obj.value
            if(keyCode==10 || keyCode == 13) // CR/LF
            return true;

This would work perfect on an onkeypress but this is what I have a stupid control like so:
<customer:usercontrol id="L_SR_searchType" CssClass="box" runat="Server" />

the HTML code generates like so if I look at the source
<input name="L_SR_searchType:textValue" type="text" value="1004434" id="L_SR_searchType_textValue" class="box" />

Is there a way to handle this somehow without changing the code behind.  For example, if I use a regular textbox and then place an onkeypress it works fine, its this damn control giving me the problem.  Here is an example of what does work fine but I have to use this control because its referenced in the code behind for things.
<asp:textbox id="L_SR_TEST" cssClass="box" onKeyPress="javascript:return enterSubmits(event, this)"

Thanks all
Question by:sbornstein2
    LVL 6

    Expert Comment

    Add attribute in code like this:
    In Page_Load()
         L_SR_Search.Attributes.Add("onKeyPress","javascript:return enterSubmits(event, this)");
    LVL 6

    Expert Comment

    To Explain:
    You can add attributes to a control.. and the result will look like this (e.g. textbox)
    <input name="name" ATTRIBUTE_NAME="ATTRIBITE_VALUE">

    Author Comment

    It is not working.  The control looks like this:

    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="usercontrol.ascx.cs" Inherits="customer.UI.usercontrol" TargetSchema=""%>
    <div><asp:ListBox Rows="1" ID="keyValue" Runat="server"  style="<%# DropStyle %>" /></div>
    <div><asp:TextBox ID="textValue" Runat="server" style="<%# TextStyle %>" /></div>

    Then in the ASPX page looks like this:
    <div id="customerdiv">
    customer:usercontrol id="L_SR_searchType" CssClass="box" runat="Server" />
    also the entersubmits javascript is there

    Then in the code behind:
    public usercontrol L_SR_searchType;
    L_SR_searchType.Attributes.Add("onKeyPress","javascript:return enterSubmits(event, this)");

    not working.  Then in the source HTML:
    <div><select name="L_SR_searchType:keyValue" size="1" id="L_SR_searchType_keyValue" class="box">
          <option value="CustomerName">Customer Name</option>
          <option value="CustomerNumber">Customer #</option>
          <option value="CustomerOrder">Customer Order</option>

    <div><input name="L_SR_searchType:textValue" type="text" id="L_SR_searchType_textValue" class="box" /></div>
    The list is getting filled also in the code behind.  So this is everything and that control is being used in many places.  I am trying to get that entersubmits to fire off.
    LVL 6

    Accepted Solution

    1- Change the control source and add OnKeyPress into your control code!
    (The one who made the control should add such thing, if not, do it yourself and add )

    2- Call your textValue directly like this:
    textValue.Attributes.Add("XXX","XXX" );

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now