在GridViewData.aspx.cs中,我有一个想要在javacsript函数内部调用的方法。
//This simple example method is from GridViewData.aspx.cs
private int ValidateNameUpdateable()
{
return 22;
}
我想在default.aspx中从Javascript调用这个函数
<script type="text/javascript">
function validateForm()
{
if (ValidateNameUpdateable==22)
{
alert("Name is not updateable, the party already started.");
return false;
}
//if all is good let it update
UpdateInsertData()
}
</script>
现在我听到了批评的声音,我主要使用jqUery,因为UpdateInsertData()是一个jquery调用,运行良好。如何使用ValidateNameUpdateable调用jQuery以从c#方法返回值。。我相信这个问题是因为我的jQuery调用,因为它只是发布,我需要做一个$.get或什么?
function ValidateNameUpdateable()
{
$(document).ready(function ()
{
$.post("GridViewData.aspx")
});
}
发布于 2011-03-03 23:48:43
看一下这个问题:Using jQuery's getJSON method with an ASP.NET Web Form
它展示了如何做到这一点。
举个例子:
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
$(function () {
$.ajax({
type: "POST",
url: "WebService.asmx/ValidateNameUpdateable",
data: "{\"input\":5}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// (msg.d is the retrieved data)
var d = msg.d;
if (d == 22) {
alert("OK");
}
else {
alert("Not OK");
}
},
error: function (msg) {
}
});
});
//-->
</script>
</body>
</html>
WebService.cs (在App_Code中):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for WebService
/// </summary>
[System.Web.Script.Services.ScriptService]
[WebService(Namespace = "http://tempuri.org/")] // <-- Put something like: services.yourdomain.com in here.
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
[WebMethod]
public int ValidateNameUpdateable(int input)
{
return input == 5 ? 22 : -1;
}
}
WebService.asmx:
<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>
我希望这能解释这个想法。
如果您希望能够传递更高级的结构(无int或string),则可能需要考虑使用JSON。
对于JSON的解析,我将使用JQuery函数parseJSON
您所需要做的就是在web服务(struct)上创建一个结构,并使用JSON序列化程序对其进行序列化。
另一个使用JSON的示例:
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
$(function () {
function getRecord(id) {
$.ajax({
type: "POST",
url: "WebService.asmx/GetRecord",
data: "{\"id\":" + id + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// (msg.d is the retrieved data)
var d = $.parseJSON(msg.d);
if (d.RecordExists) {
alert("Record with id: " + d.ID + "\nFirstName: " + d.FirstName + "\nLastName: " + d.LastName);
}
else {
alert("Record doesn't exist.");
}
},
error: function (msg) {
}
});
}
getRecord(1);
getRecord(4);
getRecord(0);
});
//-->
</script>
</body>
</html>
WebService.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
/// <summary>
/// Summary description for WebService
/// </summary>
[System.Web.Script.Services.ScriptService]
[WebService(Namespace = "http://tempuri.org/")] // <-- Put something like: services.yourdomain.com in here.
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
[Serializable]
protected class Record
{
public bool RecordExists { get; set; }
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Record() // Initializes default values
{
RecordExists = true;
ID = 0;
FirstName = "";
LastName = "";
}
}
[WebMethod]
public string GetRecord(int id)
{
// Initialize the result
Record resultRecord = new Record();
resultRecord.RecordExists = true;
resultRecord.ID = id;
// Query database to get record...
switch (id)
{
case 0:
resultRecord.FirstName = "John";
resultRecord.LastName = "Something";
break;
case 1:
resultRecord.FirstName = "Foo";
resultRecord.LastName = "Foo2";
break;
default:
resultRecord.RecordExists = false;
break;
}
// Serialize the result here, and return it to JavaScript.
// The JavaScriptSerializer serializes to JSON.
return new JavaScriptSerializer().Serialize(resultRecord);
}
}
请注意,AJAX是异步的,这意味着即使页面是以特定的顺序请求的,它们也不会以特定的顺序接收。这意味着即使您按1,4,0的顺序请求记录,它们也可以以任何顺序接收,如4,1,0或1,0,4。
发布于 2011-03-03 23:50:34
我认为您正在寻找ajax。它允许您对服务器进行异步调用并获取restult。您应该创建一个简单的aspx页面,该页面接受一些请求参数并输出正确的信息。然后使用ajax调用加载该页面并获得结果。
下面是ajax http://www.prototypejs.org/learn/introduction-to-ajax的基本概述
下面是调用http://api.jquery.com/jQuery.ajax/的jQuery ajax
发布于 2011-03-03 23:51:00
也许您的解决方案是使用load():
http://api.jquery.com/load/
https://stackoverflow.com/questions/5182971
复制相似问题