override .js

Last post 06-06-2007 11:14 PM by Benson Yu - MSFT. 6 replies.

Sort Posts:

  • override .js

    06-05-2007, 4:13 PM
    • Contributor
      2,317 point Contributor
    • nisarkhan
    • Member since 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,362

    hi there...

    i have master.page and a content pages

    in my master.page body tag i disabled the enter key which means if user HIT the enter key then it will not happend anything

    but i have one content page and i'm using multi-line textbox and i want to allow enter key to hit how do i do that? or how do i override in thei exception page?

    i have tried to use txt.attributes in the content pages thinking that i will override the .js in the master page but that does not work, any idea?

    txt.Attributes.Add("onkeydown", "if((window.event.which) ? window.event.which : event.keyCode ==13) {return true;};");

    Its all about coding!
    --------------------------
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: override .js

    06-05-2007, 4:38 PM
    • Contributor
      4,516 point Contributor
    • jackyang
    • Member since 11-16-2006, 7:27 PM
    • Waterloo, Canada
    • Posts 615

    How about give masterpage a property if disable enterkey is true, then in your content page, toggle it. With default true for most of your pages.

    Jack Yang
    .NET Developer
  • Re: override .js

    06-05-2007, 4:45 PM
    • Contributor
      2,317 point Contributor
    • nisarkhan
    • Member since 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,362

    if possible can you show me few lines of code, just to get an idea?

    thanks

    Its all about coding!
    --------------------------
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: override .js

    06-05-2007, 4:58 PM
    • Contributor
      4,516 point Contributor
    • jackyang
    • Member since 11-16-2006, 7:27 PM
    • Waterloo, Canada
    • Posts 615
    In your masterpage, make <body> tag runat server:

    <body runat="server" id="masterpagebody">

    ---------- in your master page codebehind
    // private variable
    private bool _enableEnterKey;  // default false
    
    // public property
    public bool EnableEnterKey
    {
        set
        {
            _enableEnterKey = value;
        }
    }
    
    // method or event
    private setEnterKeyAction
    {
        if (!_enableEnterKey)
        {
           // find the body control first
           masterpagebody.Attribute.Add("onload", "javascript here disable enterkey");
        }
    }
    
    ---------- in your content page, add this page directive: 
    

    <%@ MasterType VirtualPath="~/yourmasterpage.master" %> 

     

     

    ---------- in the content page codebehind
    // event
    protected void Page_Load()
    {
        Master.EnableKeyAction = true;
    }
     
     
    Jack Yang
    .NET Developer
  • Re: override .js

    06-05-2007, 9:39 PM
    • Contributor
      2,317 point Contributor
    • nisarkhan
    • Member since 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,362

    thank you for your reply and i think i have one more concern and which is

    i have coulpe of textbox in the content page and i want to enable enter key only on the textbox which have multline

    not for all, how do i take care that?

    thanks again.

    Its all about coding!
    --------------------------
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: override .js

    06-05-2007, 10:07 PM
    • Contributor
      4,516 point Contributor
    • jackyang
    • Member since 11-16-2006, 7:27 PM
    • Waterloo, Canada
    • Posts 615

    If you want user type some stuff in the multiline textbox, say ID="txtMulti", and you have the button with ID="btnPost". ASP 2.0 supports Panel control with DefaultButton property, you may want to include the multiline textbox and the button inside <asp:Panel>. So it looks like the following:

     

    <asp:Panel runat="server" DefaultButton="btnPost">
        <asp:TextBox runat="server" ID="txtMulti" />
        <asp:Button runat="server" ID="btnPost" />
    </asp:Panel>

    This would be the easiest solution.

     

    Or if you don't want to define a defaultbutton for some reason, you can do the following workaround for this textbox and button pair. First you need to ensure the masterpage's body enabled enterkey. Next is in the codebehind, inside a page event e.g.: Page_Load, add an javascript attribute to the textbox control:

    protected void Page_Load()
    {
        // some code.....
    
        StringBuilder sb = new StringBuilder();
        sb.Append("if(event.which || event.keyCode) {");
        sb.Append(    "if ((event.which == 13) || (event.keyCode == 13)) {");
        sb.Append(        "document.getElementById('");
        sb.Append(         btnPost.UniqueID);
        sb.Append(         "').click();");
        sb.Append(         "return false;");
        sb.Append(    "}");
        sb.Append("}");
        sb.Append("else {");
        sb.Append(    "return true;");
        sb.Append("}");
        string js = sb.ToString();
        txtMulti.Attributes.Add("onkeydown", js);
    }
    
     
    Jack Yang
    .NET Developer
  • Re: override .js

    06-06-2007, 11:14 PM
    Answer

    Hi nisarkhan,

    Based on my understanding, your concern is to enable Enter key for the TextBox controls which are set to MultiLine. If I have misunderstood your concern, please let me know.

    My idea is to estimate the event source element in your prevent Enter key function. If the element is “TEXTAREA”, don’t prevent. The following code is for your reference.

    *********//the master page
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body onkeydown="return disableEnterKey()">
        <form id="form1" runat="server">
            <asp:Label ID="Label1" runat="server" Text="Test disable Enter Key"></asp:Label>
            <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>
        </form>
    </body>
    <script type="text/javascript">
        function disableEnterKey()  
        {
           var key = window.event.keyCode;
            if (key ==13)
            {
                if (window.event.srcElement.tagName != 'TEXTAREA')
                {
                    alert('Enter key is disabled');
                return false;   
                }
            }
        }
    </script>
    </html>
     
    *********//aspx page
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" ></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </asp:Content>

     

    Sincerely,
    Benson Yu
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. This can be beneficial to other community members reading the thread.
Page 1 of 1 (7 items)