Div onload功能和嵌入服务器代码c#

时间:2012-09-26 12:40:24

标签: c# javascript asp.net

我试图在.aspx页面中调用c#属性,其中myfunc是jquery函数。 这个函数接受我通过c#公共属性传递的参数。我想在div渲染后立即调用此函数。以前我已经在ascx控件中的prerender事件上完成了这个。对于下面的代码,wha是精确的ssyntax,从这里调用jquery函数是有效的吗?

    <div  class ="v" onload ="Javascript:myfunc('<%= this.articleID  %> '      
     ,'" +<%=this.UserID %>+"','" +<%=this.EncryptedUserID %>" +','" +<%# 
     this.ItemIndex  %>)">

感谢

被修改

好的,如果我喜欢这样的话

            <div  class ="v"  >
                         <script language ="javascript" >

                          JGetTotalVotes(<%= this.articleid  %>  ,<% 
              this.ThisEncryptedUserID %>,<% this.ItemIndex  %>,'aaa');
                          </script>

我不想要服务器端隐藏字段解决方案,如果我使用hiden字段(非服务器html标记),那么我仍然需要调用&lt; %%&gt;从服务器端获取值到hiddent字段

2 个答案:

答案 0 :(得分:1)

如果不是问题,我发现你在最后一个参数后缺少单引号。 您可以使用隐藏字段并在javascript中调用该函数

将此添加到您的页面

<asp:HiddenField runat="server" ID="hdnArticleId" ClientIDMode="Static" />
<asp:HiddenField runat="server" ID="hdnUserId" ClientIDMode="Static"/>
<asp:HiddenField runat="server" ID="hdnEncryptedUserID" ClientIDMode="Static"/>
<asp:HiddenField runat="server" ID="hdnItemIndex" ClientIDMode="Static"/>

绑定Page_Load事件中的那些隐藏字段记得添加ClientIDMode =“Static”来设置id,因为你设置的不是asp.net生成的id,以便能够在javascript中使用它们

添加到您的javascritp

function YourFunction(){
     var ArticleId = document.getElementById("hdnArticleId").value;
     var UserId= document.getElementById("hdnUserId").value;
     var EncryptedUserID= document.getElementById("hdnEncryptedUserID").value;
     var ItemIndex= document.getElementById("hdnItemIndex").value;
     // your code goes here
}

并且不要忘记在页面上调用你的javascript函数

答案 1 :(得分:0)

Div元素不会引入外部内容,因此它们没有加载事件。

如果你想在解析div元素后运行一些JS:

<div></div>
<script> foo(); </script>
相关问题