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

Pass asp control as parameter to javascript - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 5

You 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);
发布评论

评论列表(0)

  1. 暂无评论