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

asp.net - Pass variables to javascript method from code behind - Stack Overflow

programmeradmin0浏览0评论

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 the function "DisplayValues();" ?

I have the javascript function is aspx as below:

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

Thanks

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 the function "DisplayValues();" ?

I have the javascript function is aspx as below:

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

Thanks

Share Improve this question edited Jan 10, 2011 at 13:59 Brian Mains 50.7k35 gold badges155 silver badges260 bronze badges asked Jan 10, 2011 at 13:53 SanSan 1,8377 gold badges33 silver badges57 bronze badges 2
  • You should learn proper web development before jumping to the "short cuts" dot provides – Itay Moav -Malimovka Commented Jan 10, 2011 at 13:57
  • 1 I think a better ment would be, could you explain why you chose to do it this way not not just put that in the aspx page? – Steve Commented Jan 10, 2011 at 13:59
Add a ment  | 

6 Answers 6

Reset to default 1

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 Microsoft has some special js function $get,$find that translate asp 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)

  1. 暂无评论