【发布时间】:2015-05-27 21:23:01
【问题描述】:
success JavaScript 函数中的 alert(result) 显示一个对话框,该对话框仅包含页面的整个 HTML 代码。这很奇怪。我无法让客户端在 C# 中调用服务器端函数。
这意味着打印服务器端函数中给出的字符串。
提交.aspx
<!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>
<script type='text/javascript'>
function GetName() {
PageMethods.Name(Success, Failure);
}
function Success(result) {
alert(result);
}
function Failure(error) {
alert(error);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID='ScriptManager1' runat='server' EnablePageMethods='true' />
<div>
<asp:Button ID="Button1" runat="server" Text="Get Name" OnClientClick='GetName();return false;' />
</div>
</form>
</body>
</html>
提交.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Drawing.Layout;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization;
//needed for SQL access
using System.Data.SqlClient;
using System.Collections;
using System.Net;
using PDFCreator;
namespace theform
{
public partial class submit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//functions fun = new functions();
//fun.Main();
}
[WebMethod]
public static string Name()
{
string Name = "Hello Rohatash Kumar";
return Name;
}
// <input id="clickMe" type="button" value="clickme" onclick="create_array();" />
}
}
【问题讨论】:
-
我想你可能遗漏了一些东西,我试过你的代码,它工作正常。
-
Visual Studio 中是否有一些选项,或者需要启用的服务器?
标签: javascript c# asp.net client-side server-side