Click to See Complete Forum and Search --> : using javascript in master/content pages


manared
06-26-2006, 03:19 PM
I am using vb.net 2005 with ASP to create a website. I have used a master page with several content pages. I need to know how to use javascript on the content page. Do I have to put it in the content or master page and how do I call it? I can get the javascript function to work on a regular .net/asp page, but not with the master/content pages. I've posted in several other forums and no one seems to know how to do this. Anybody have suggestions or know how to do this?

ANY help would be greatly appreciated. I've tried many different things and I keep getting this error on the page saying that my textbox is undefined. (I am trying to count the characters on this textbox on onkeyup)

manared
06-27-2006, 12:28 PM
Alright, here's what I have in my master page: (javascript code)

HTML Code:

<head runat="server">
<title>Free Master Page</title>
<script type="text/javascript">
function textCounter(field, countfield, maxlimit) {

if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.innerText = maxlimit - field.value.length + " characters remaining";
}

function doPaste(control) {
maxLength = control.attributes["maxLength"].value;
value = control.value;
if(maxLength){
event.returnValue = false;
maxLength = parseInt(maxLength);
var oTR = control.document.selection.createRange();
var iInsertLength = maxLength - value.length + oTR.text.length;
var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
oTR.text = sData;
}
}

</script>
</head>

Then here's what I have in my content page:

Code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtMessage.Attributes.Add("onkeyup", "textCounter(" & txtMessage.ClientID & ", " & lblCharacters.ClientID & ", 99);")
txtMessage.Attributes.Add("onpaste", "doPaste(" & txtMessage.ClientID & ");")
'Other code
End Sub

I am getting an error about the contentplaceholder_lblCharacters being undefined. Why would it recognize the txtMessage.ClientID and not the lblCharacters.ClientID? It seems to define the txtMessage, but not lblCharacters. Does anyone have any clue about this or have any other ideas on how I can accomplish this task?