Pass asp.net value to javascript block

I need to pass the values ​​from my asp.net code to the javascript code block on the page.

I know at least two ways to do this:

<script runat=server> int a = 42; protected override void OnInit(EventArgs e) { lbl.Text = "42"; } </script> <script> var a = <%= a %>; var b = <asp:Literal runat=server id=lbl>/>; alert("the meaning of life=" + a + " or " + b); </script> 

But is there a better way in asp.net WebForms to associate a variable value in asp.net code with a javascript block? Both of them seem messy because they do not support intellisense.

+4
source share
5 answers

In the end, I did something that I consider more reliable. On the main page, I added the following:

 public Dictionary<string, object> JavaScriptVars { get; set; } 

Then I add the result of this function to the page:

 public string GetJavaScriptVars() { if (JavaScriptVars.Count == 0) return null; StringBuilder sb = new StringBuilder(); sb.Append("<script>"); System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); int i = 0; foreach (string s in JavaScriptVars.Keys) { if (i == 0) sb.Append("var "); sb.Append(s + "="); object o = JavaScriptVars[s]; if (o is System.Collections.Specialized.NameValueCollection) { Dictionary<string, string> dict = new Dictionary<string, string>(); foreach (string key in ((System.Collections.Specialized.NameValueCollection)o).Keys) dict[key] = ((System.Collections.Specialized.NameValueCollection)o)[key]; o = dict; } sb.Append(serializer.Serialize(o)); sb.Append(i == JavaScriptVars.Count - 1 ? ";" : ","); i++; } sb.Append("</script>"); return sb.ToString(); } 

So now I can do this:

 JavaScriptVars["Name"] = "John"; //string JavaScriptVars["Age"] = 32; //int JavaScriptVars["Hobbies"] = new string[] { "Sewing", "Hiking" }; //array 

and on my last page I get:

 <script> var Name='John',Age=32,Hobbies=["Sewing", "Hiking"]; </script> 

So now I have a reliable way to pass values ​​with properly managed page types on a page without script blocks or labels.

+2
source

As a rule, the best solution is to save the value in a hidden field on the page:

 var el = document.getElementById("<%=HiddenField1.ClientID%>"); if (el){ var value = el.value; } 

You can also learn PageMethods :
http://geekswithblogs.net/WillSmith/archive/2008/12/09/asp.net-pagemethods-to-the-rescue.aspx

+1
source

Add a protected property to your code and access it from javascript:

Server:

 Protected ReadOnly Property MeaningOfLife() As Int32 Get Return 42 End Get End Property 

customer:

 <script> var a = <%= Me.MeaningOfLife %>; var b = <asp:Literal runat=server id=lbl>/>; alert("the meaning of life=" + a + " or " + b); </script> 
0
source

In your example, I would stick with var a = <%= a %>;

In case the value of a, the .NET value, has some connection with the element on the page, you can put this value in the element itself. For example, if you have a table row on a page with additional data that will use javascript, but the data in the context of the row.

 <tr data-myvalue='<%=a%>'></tr> 

Then you can use javascript (possibly jQuery) for an extra data value, instead of putting a bunch of disabled variables and values ​​in a <script> block

0
source

Declare a variable in your code for the file, for example:

 string s; 

You can fill this variable in any way. Now I can access this variable in javascript, as shown below. (if you need to pass a string, you must specify the delimiters "or"):

 var a = '<%= a %>'; 
0
source

Source: https://habr.com/ru/post/1381330/


All Articles