开发者

Pass variables to javascript method from code behind

I am calling a javascript function from .cs file which is as below:

private string CallValuesScript()
{
     StringBuilder sb = new StringBuilder();
     sb.AppendLine("$(document).ready(function() {");
     sb.AppendLine("DisplayValues();");
     sb.AppendLine(" });");
     return sb.ToString();
}

I have two integers declared at class level private int ratingLinkId = 0 ; private int ratingValue = 0;

Please let me know how I can pass these two integer values to开发者_如何学JAVA the function "DisplayValues();" ?

I have the javascript function is aspx as below:

 function DisplayRatings(id, count) {  
//code
}

Thanks


You can use

sb.AppendLine("DisplayValues(" + ratingLinkId + "," + ratingValue + ");");


You can pass server-side variables to client side by appending them to the method call in your script helper.

sb.AppendLine("DisplayValues(" + ratingLinkId.ToString() + ", " + ratingValue.ToString() + ");")


You want to avoid generating JavaScript in codebehind. I also personally don't like embedded code blocks within my javascript. Furthermore, using hidden fields in your HTML would mean those values end up being submitted on form submit.

My solution is to create a private internal class in the codebehind with properties for all the values you'll need in JavaScript:

class JSValues {
    public int id { get; set; }
    public int count { get; set; }
}

In codebehind, serialize this into JSON:

string json = new JavaScriptSerializer().Serialize(new JSValues() {
    id = 0,
    count = 0 // or whatever you want to assign them
});

ClientScript.RegisterClientScript(
    this.GetType(),
    "codeBehindToJS",
    "var fromCodeBehind = " + json + ";");

Finally, in javascript, these values may be accessed:

DisplayValues(fromCodeBehind.id, fromCodeBehind.count);

EDIT: Some corrections, even though it's a bit late to the party.


The solution using $get didn't work for me....

But here is a easy way to do it without using a hidden field.

In your code behind define your variable:

Protected maxPhotos As String

Then on your aspx page inside your javascript simply grab it like so:

var maxPhotos = 0 + "<%=maxPhotos%>";

I did the "0+ " because I wanted to force conversion from string to number.


One method would be to have Hidden Fields in the aspx that contain the values. That would only ready work if the variables are not going to change since the page has loaded.

Another thing you could do is have a page method with the WebMethod / ScriptMethod attribute and call it as a web service call from the javascript.

Another method would be to pass the variables into the javascript function when you are building the javascript string


@San, I agree with Wraith. somewhere in your initialization code, (aspx page markup, page_load, etc..) create to hidden fields and set their values to the integers you want. After you do that you will no longer need to create your js with code behind.

here is a very simple example, I've set the hiddenfields with literals. You could just as easily leave the values unset, and set them in some initialization code in your .cs page.

Add these lines to your .aspx page

<asp:HiddenField runat="server" id="hdnfldarg1" Value="1" />
<asp:HiddenField runat="server" id="hdnfldarg2" Value="2" />

now change your js code to the following. Assuming you are using a newer version of asp.net Microsoft has some special js function $get,$find that translate asp.net id to clientid.

$(document).ready(function() {
var arg1 = $get('<% hdnfldarg1 %>'); 
var arg2 = $get('<% hdnfldarg2 %>');
DisplayValues(arg1,arg2);") 
 });"

Of course this is untested code. You could add whatever Page_load/init code to set the exact values of the two hidden fields. I'm not sure this will work at all if you try to use a separate js file

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜