I would like to have this function:
function count(inputObj, ouputObj)
{
outputObj.value = inputObj.value.length;
}
And I would like to call it in this way:
<asp:TextBox ID="txtUSERGUID" runat="server" onkeyup="count(this, document.getElementById('<%=txtUSERGUIDLength.ClientID%>'));" onkeydown="count(this, document.getElementById('<%=txtUSERGUIDLength.ClientID%>'));" />
<asp:TextBox ID="txtUSERGUIDLength" runat="server" />
<asp:Label ID="lblUSERGUIDLength" runat="server" Text="chars" />
But I keep getting a javascript error that says: 'outputObj' is undefined. Can I call this function like this or am I going the pletely wrong direction?
I would like to have this function:
function count(inputObj, ouputObj)
{
outputObj.value = inputObj.value.length;
}
And I would like to call it in this way:
<asp:TextBox ID="txtUSERGUID" runat="server" onkeyup="count(this, document.getElementById('<%=txtUSERGUIDLength.ClientID%>'));" onkeydown="count(this, document.getElementById('<%=txtUSERGUIDLength.ClientID%>'));" />
<asp:TextBox ID="txtUSERGUIDLength" runat="server" />
<asp:Label ID="lblUSERGUIDLength" runat="server" Text="chars" />
But I keep getting a javascript error that says: 'outputObj' is undefined. Can I call this function like this or am I going the pletely wrong direction?
Share Improve this question asked Jun 8, 2009 at 18:46 swolff1978swolff1978 1,8657 gold badges28 silver badges44 bronze badges3 Answers
Reset to default 5You can't have this inside of a server tag it won't execute the server side code:
<%=txtUSERGUIDLength.ClientID%>
If you are going to do it do something like
<script language='javascript'>
var g_clientID = '<%=txtUSERGUIDLength.ClientID%>'
<script/>
then do:
onclick(this, document.getElementById(g_clientID))
Check your page source, I think you'll find that the "onkeyup" event handler still has the server side code in it. Since you are setting a property on a server control, the "<%= %>" code is not executed. You'll have to move this to a separate javascript function if you want to do this.
I don't believe you can insert parameters in this manner for server side controls.
You can set these parameters in your server side code:
txtUSERGUID.Attributes.Add("onkeyup", yourEventHandlerScript);