开发者

pass a c# variable to javascript function

I'm trying to pass a c# variable as an argument to a java-script function like

<%var count=model.SomeMode.Count();%>

when i pass it to my java script function "checkAll(count)" it does not fire but without the argument its workin fine

<a  href="#" onclick="return checkAl开发者_运维知识库l(count);">CheckAll</a>


Remember the page gets created however you wish. So, you could do something like:

<script type="text/javascript">
  var count = <%=model.SomeMode.Count(); %>;
</script>

You could apply the same logic in method calls, so you could do:

<a  href="#" onclick="return checkAll(<%=model.SomeMode.Count(); %>);">CheckAll</a>


Javascript can't see your C# source directly - you need to write it into the Javascript source on the server side:

<a  href="#" onclick="return checkAll(<%= count %>);">CheckAll</a>


It should look like this:

<script type="text/javascript">
  var count=<%=model.SomeMode.Count()%>;
</script>

Currently you're declaring your variable in C#, not outputting it in the page as a JavaScript one. Instead, you want to declare var count literally in the page and have it set to the output of model.SomeMode.Count().


This might be good for your solution:

<a  href="#" onclick='return checkAll(<%=count%);'>CheckAll</a>

But there is an other way of doing this instead of conventional way:

Sharing Variables Between JavaScript and C# by Fredrik Kalseth

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜