将 C# 数据结果传递给 JavaScript 变量
Pass C# Data Result to JavaScript Variable
我需要将下面的 SQL 结果传递给 ASP.Net 中的 Javascript?我试图在 JS 中声明这两个字段,但无法正确声明。我如何在 JS 中获取结果?
var Description = "<%=this.Description%>"
var ApplicationSourceCount = "<%=this.ApplicationSourceCount%>"
在 C# 中声明了字符串
public class ApplicantSourceData
{
public string Description { get; set; }
public string ApplicantSourceCount { get; set; }
}
C# WebMethod
[WebMethod]
public List<ApplicantSourceData> GetApplicantSourceData(List<string> aData)
{
//SqlDataReader reader;
List<ApplicantSourceData> GetApplicantSourceData = new List<ApplicantSourceData>();
string connectionString = ConfigurationManager.ConnectionStrings["ATL2"].ConnectionString;
string commandTextApplicantsByMonthCount = Properties.Queries.commandTextApplicantsByMonthCount;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(commandTextApplicantsByMonthCount))
{
command.CommandText = commandTextApplicantsByMonthCount;
command.CommandType = CommandType.Text;
command.Connection = con;
con.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
int counter = 0;
while (reader.Read())
{
ApplicantSourceData tsData = new ApplicantSourceData();
tsData.Description = reader["Description"].ToString();
tsData.ApplicantSourceCount = reader["ApplicantSourceCount"].ToString();
GetApplicantSourceData.Add(tsData);
counter++;
}
}
}
return GetApplicantSourceData;
}
}
我已经尝试了以下方法,但没有
您应该从客户端调用 WebMethod(例如,作为 AJAX 请求)。然后,您应该让 WebMethod return 使用请求的数据(例如,作为 JSON 格式的字符串)对客户端做出响应。
有几种方法可以做到这一点。第一个是使用 AJAX.
示例:
C#:
[WebMethod]
public static string someWebMethod(String data) {
// your code here
var returnData = /* some kind of data */
JavaScriptSerializer json = new JavaScriptSerializer();
return json.Serialize(returnData);
}
JS(使用 jQuery 作为示例,但您也可以在常规 JS 中执行此操作):
$.ajax({
type: "POST",
url: "PageName.aspx/someWebMethod",
data: "{"data": data}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
// Do something with the response.
}
});
另一种选择是使用 PageMethods
。为此,您可以按照如下模式从 JS 调用该方法:
PageMethods.someWebMethod(data);
function onSucess(result) {
console.log(result);
}
function onError(result) {
console.log(result);
}
我建议您也多看看 ASP.NET WebMethod 文档。此外,这里有一些教程可能会有所帮助:Calling ASP.NET WebMethod using jQuery-AJAX and Calling an ASP.NET C# Method (Web Method) Using JavaScript.
我需要将下面的 SQL 结果传递给 ASP.Net 中的 Javascript?我试图在 JS 中声明这两个字段,但无法正确声明。我如何在 JS 中获取结果?
var Description = "<%=this.Description%>"
var ApplicationSourceCount = "<%=this.ApplicationSourceCount%>"
在 C# 中声明了字符串
public class ApplicantSourceData
{
public string Description { get; set; }
public string ApplicantSourceCount { get; set; }
}
C# WebMethod
[WebMethod]
public List<ApplicantSourceData> GetApplicantSourceData(List<string> aData)
{
//SqlDataReader reader;
List<ApplicantSourceData> GetApplicantSourceData = new List<ApplicantSourceData>();
string connectionString = ConfigurationManager.ConnectionStrings["ATL2"].ConnectionString;
string commandTextApplicantsByMonthCount = Properties.Queries.commandTextApplicantsByMonthCount;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(commandTextApplicantsByMonthCount))
{
command.CommandText = commandTextApplicantsByMonthCount;
command.CommandType = CommandType.Text;
command.Connection = con;
con.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
int counter = 0;
while (reader.Read())
{
ApplicantSourceData tsData = new ApplicantSourceData();
tsData.Description = reader["Description"].ToString();
tsData.ApplicantSourceCount = reader["ApplicantSourceCount"].ToString();
GetApplicantSourceData.Add(tsData);
counter++;
}
}
}
return GetApplicantSourceData;
}
}
我已经尝试了以下方法,但没有
您应该从客户端调用 WebMethod(例如,作为 AJAX 请求)。然后,您应该让 WebMethod return 使用请求的数据(例如,作为 JSON 格式的字符串)对客户端做出响应。
有几种方法可以做到这一点。第一个是使用 AJAX.
示例:
C#:
[WebMethod]
public static string someWebMethod(String data) {
// your code here
var returnData = /* some kind of data */
JavaScriptSerializer json = new JavaScriptSerializer();
return json.Serialize(returnData);
}
JS(使用 jQuery 作为示例,但您也可以在常规 JS 中执行此操作):
$.ajax({
type: "POST",
url: "PageName.aspx/someWebMethod",
data: "{"data": data}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
// Do something with the response.
}
});
另一种选择是使用 PageMethods
。为此,您可以按照如下模式从 JS 调用该方法:
PageMethods.someWebMethod(data);
function onSucess(result) {
console.log(result);
}
function onError(result) {
console.log(result);
}
我建议您也多看看 ASP.NET WebMethod 文档。此外,这里有一些教程可能会有所帮助:Calling ASP.NET WebMethod using jQuery-AJAX and Calling an ASP.NET C# Method (Web Method) Using JavaScript.