这个问题在这个网站上已经讨论过很多次了,但是我没有找到解决问题的方法。
我执行了以下步骤。
1.创建了一个JS函数.
 function showModal() {
            alert("called");
       }2.在.aspx文件中添加了一个脚本管理器
 <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>3.在.cs文件中创建了一个方法
 [System.Web.Services.WebMethod()]
        [System.Web.Script.Services.ScriptMethod()] 
        protected void register_user(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "showModal", "javascript:showModal(); ", true);
            log.Debug("register_user is called");
        }但它并没有调用JS函数。
发布于 2016-10-14 08:12:35
可能是因为,您还没有关闭javascript函数:
function showModal() {
            alert("called");
}或者你也可以试试这个:
替换您的这一行:
Page.ClientScript.RegisterStartupScript(this.GetType(), "showModal", "javascript:showModal(); ", true);使用
Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", "showModal(); ", true); // correct发布于 2016-10-14 08:15:14
你混合了这么多东西。
如果您想将代码后面的javascript添加到您的html中。这应该位于某些全局事件Page_Load或PreRender中。
Page.ClientScript.RegisterStartupScript(this.GetType(), "showModal", "YourJavascriptCode", true);如果要从AJAX调用调用aspx中的方法,则应返回响应。
    [System.Web.Services.WebMethod()]
    [System.Web.Script.Services.ScriptMethod()] 
    public static string register_user(object sender, EventArgs e)
    {
         //you can return a class->Json
         return stuff;
    }如果要在客户端执行javascript,请单击按钮。我想你想要这个!
    YourButton.Attributes["onclick"]= "javaScriptFunction()";
    //if you want to not execute server side
    YourButton.Attributes["onclick"]= "javaScriptFunction(); return false";这里是向asp按钮添加javascript函数的最简单方法。
<asp:Button ID="RegisterBtn" runat="server" Text="Register"  OnClientClick="showModal()" onclick="register_user"/>发布于 2016-10-14 08:26:02
首先,我不认为从aspx.cs调用Js函数是个好主意。
总之,从你的片段中,只需更改如下:
Page.ClientScript.RegisterStartupScript(this.GetType(), "showModal", "showModal()", true);注意第三个参数:"showModal()"
另外,您的JS函数"showModal()",它通过单击这个按钮工作吗?<input type="button" value="Submit" onclick="showModal();"/>
https://stackoverflow.com/questions/40038064
复制相似问题