Zara_me
Zara_me

Reputation: 268

Div onload function and embedding server code c#

I am trying to call c# properties in .aspx page, where myfunc is jquery function. this function take parameters which i am passing through c# public properties. and I want to call this function as soon as div is rendered.previously i have done this on prerender event in ascx control. wha is exact ssyntax for below code and calling jquery function from here is valid?

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

thanks

Edited

Ok If i do like this way

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

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

I do not want server side hidden fields solutions and if i use hiden field ( non server html tag) then i still need to call <%%> to fetch value from server side to hiddent field

Upvotes: 1

Views: 1626

Answers (2)

Mohamed Salah
Mohamed Salah

Reputation: 975

I see you are missing single quote after the last parameter if that is not the problem. You can use hidden fields and call the function in javascript

Add this to your page

<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"/>

bind those hiddenfields in Page_Load event remember to add ClientIDMode="Static" to set the id as you set not a generated one by asp.net to have the ability to use them in javascript

Add To your 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
}

and don't forget to call your javascript function in page onload

Upvotes: 1

Quentin
Quentin

Reputation: 943650

Div elements don't pull in external content, so they don't have load events.

If you want to run some JS after a div element has been parsed:

<div></div>
<script> foo(); </script>

Upvotes: 0

Related Questions