开发者

How can I write javascript functions in content page html and how I call that function

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="totalm开发者_如何转开发sg" runat="server" Text="0"></asp:Label>

</asp:Content>


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜