最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - How can I write javascript functions in content page html and how I call that function - Stack Overflow

programmeradmin0浏览0评论

I write two javascript functions in content page. Then I take a html textbox and on the onkeypress event I try to call those two functions, but I am running that application and I didn't find any output to help me. Here I am trying to count number of characters in textbox on keypress event. If I write a javascript function in simple page then it runs successfully but it doesn't run in content page help me.

Here is the code

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile ="~/Site1.Master"  CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<asp:Content ID="Content1" runat="server" 
    contentplaceholderid="ContentPlaceHolder1">

    <script language ="javascript" type = "text/javascript">
    maxL=0;
 var bName = navigator.appName;

    function taCount(taObj,Cnt,totmsg) 
    { 
        objCnt=createObject(Cnt);
        objtotmsg = createObject(totmsg);
        objVal=taObj.value;
        if (objCnt) 
        {
            if(bName == "Netscape")
            {   
                //objCnt.textContent = maxL-objVal.length;}
                var totalchar = parseInt((objVal.length - 1) / 160);
                objCnt.textContent = maxL + objVal.length;
                objtotmsg.textContent = totalchar + 1;
            }
            //else{objCnt.innerText= maxL -objVal.length;}
            else
            {
                var totalchar = parseInt((objVal.length - 1) / 160);
                objCnt.innerText= maxL + objVal.length;
                objtotmsg.innerText = totalchar + 1;
            }
        }
        return true;
    }
    function createObject(objId) 
    {
        if (document.getElementById) return document.getElementById(objId);
        else if (document.layers) return eval("document." + objId);
        else if (document.all) return eval("document.all." + objId);
        else return eval("document." + objId);
    }

    </script> 
<textarea id="TextArea1" onkeyup="return taCount(this,'charcount','totalmsg')" cols="20" rows="10"></textarea>

    <asp:Label ID="charcount" runat="server" Text="0"></asp:Label>/<asp:Label ID="totalmsg" runat="server" Text="0"></asp:Label>

</asp:Content>

I write two javascript functions in content page. Then I take a html textbox and on the onkeypress event I try to call those two functions, but I am running that application and I didn't find any output to help me. Here I am trying to count number of characters in textbox on keypress event. If I write a javascript function in simple page then it runs successfully but it doesn't run in content page help me.

Here is the code

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile ="~/Site1.Master"  CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<asp:Content ID="Content1" runat="server" 
    contentplaceholderid="ContentPlaceHolder1">

    <script language ="javascript" type = "text/javascript">
    maxL=0;
 var bName = navigator.appName;

    function taCount(taObj,Cnt,totmsg) 
    { 
        objCnt=createObject(Cnt);
        objtotmsg = createObject(totmsg);
        objVal=taObj.value;
        if (objCnt) 
        {
            if(bName == "Netscape")
            {   
                //objCnt.textContent = maxL-objVal.length;}
                var totalchar = parseInt((objVal.length - 1) / 160);
                objCnt.textContent = maxL + objVal.length;
                objtotmsg.textContent = totalchar + 1;
            }
            //else{objCnt.innerText= maxL -objVal.length;}
            else
            {
                var totalchar = parseInt((objVal.length - 1) / 160);
                objCnt.innerText= maxL + objVal.length;
                objtotmsg.innerText = totalchar + 1;
            }
        }
        return true;
    }
    function createObject(objId) 
    {
        if (document.getElementById) return document.getElementById(objId);
        else if (document.layers) return eval("document." + objId);
        else if (document.all) return eval("document.all." + objId);
        else return eval("document." + objId);
    }

    </script> 
<textarea id="TextArea1" onkeyup="return taCount(this,'charcount','totalmsg')" cols="20" rows="10"></textarea>

    <asp:Label ID="charcount" runat="server" Text="0"></asp:Label>/<asp:Label ID="totalmsg" runat="server" Text="0"></asp:Label>

</asp:Content>
Share Improve this question edited Aug 14, 2010 at 15:23 Kris van der Mast 16.6k8 gold badges41 silver badges64 bronze badges asked Aug 14, 2010 at 12:35 NeoNeo 513 silver badges5 bronze badges 1
  • Accept the answer, since you also bothered to leave a ment saying it solved your problem. – Jeroen Commented Aug 14, 2010 at 14:42
Add a ment  | 

1 Answer 1

Reset to default 4

This is because the IDs get lengthened (their naming containers are pre-pended) when inside a master page, so instead of this:

taCount(this,'charcount','totalmsg')

You'll need this, which gets their actual rendered IDs:

taCount(this,'<%=charcount.ClientID %>','<%=totalmsg.ClientID %>')

If you view source in the browser and search for charcount or totalmsg when rendered in that master page, you'll see what I mean about the IDs, they'll probably look something like this: _ctl00_Content1_charcount.

发布评论

评论列表(0)

  1. 暂无评论