问:
Hi,

I have this C#/ASP.NET/.aspx page with a string variable named myString with the value "Mario Gamito".

How can I access "Mario Gamito" from within a JavaScript function, i. e., the variable's value ?

Already tried '" + mystring + "' but it doesn't works.

Any help would be appreciated.

答1:

JavaScript 访问 c#变量的方法<!-- Default2.aspx -->

JavaScript 访问 c#变量的方法using System;
JavaScript 访问 c#变量的方法
using System.Collections;
JavaScript 访问 c#变量的方法
//Default2.aspx.cs
JavaScript 访问 c#变量的方法
using System.Configuration;
JavaScript 访问 c#变量的方法
using System.Data;
JavaScript 访问 c#变量的方法
using System.Linq;
JavaScript 访问 c#变量的方法
using System.Web;
JavaScript 访问 c#变量的方法
using System.Web.Security;
JavaScript 访问 c#变量的方法
using System.Web.UI;
JavaScript 访问 c#变量的方法
using System.Web.UI.HtmlControls;
JavaScript 访问 c#变量的方法
using System.Web.UI.WebControls;
JavaScript 访问 c#变量的方法
using System.Web.UI.WebControls.WebParts;
JavaScript 访问 c#变量的方法
using System.Xml.Linq;
JavaScript 访问 c#变量的方法
JavaScript 访问 c#变量的方法
public partial class Default2 : System.Web.UI.Page


答2:

you can also try pushing the variable to javascript using the registerClientScriptBlock

JavaScript 访问 c#变量的方法string jsVariable = string.Empty;
JavaScript 访问 c#变量的方法
string mySting = "Mario Gamito";
JavaScript 访问 c#变量的方法
JavaScript 访问 c#变量的方法jsVariable 
= "var myjsString = " + myString;
JavaScript 访问 c#变量的方法
JavaScript 访问 c#变量的方法
JavaScript 访问 c#变量的方法
this.Page.ClientScript.RegisterClientScriptBlock(
JavaScript 访问 c#变量的方法                    
this.GetType(), "YourStringVar", jsVariable, true);
JavaScript 访问 c#变量的方法

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-26
  • 2022-12-23
  • 2021-08-19
  • 2022-01-28
  • 2021-08-31
  • 2022-12-23
相关资源
相似解决方案