【问题标题】:Client Side function returning HTML Code instead of Server Side return value客户端函数返回 HTML 代码而不是服务器端返回值
【发布时间】: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


【解决方案1】:

你可以使用onclick按钮事件来显示警报吗?这是另一种方法

ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Insert is successfull')", true);

【讨论】:

    猜你喜欢
    • 2015-11-22
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2015-12-07
    • 2018-12-14
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多