从ASP.net调用ScriptManager.RegisterStartupScript

问题描述 投票:0回答:3

从ASP.net调用javascript函数时遇到问题;我看到的所有样本都在按钮单击事件中,但是我想检查Page_load中的内容,如果结果为FALSE,则向用户显示警报并重定向到另一个页面。我尝试了这些代码,但它们都没有破坏,页面重定向没有任何警报

1.

protected void Page_PreRender(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey", 
    "alert('my message');", true);
}

2.

   protected void Page_Load(object sender, EventArgs e)
    {
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert","alert('my message');", true);
                    Response.Redirect("~/register.aspx", false);
                    return;
  }

我也试过“ScriptManager.RegisterClientScriptBlock”,但它没有用。提前致谢

javascript asp.net scriptmanager
3个回答
0
投票

如果你想用javascript检查页面加载的东西,那么使用

$(document).ready(function () {
    //Your code here
} );

或者在javascript中

window.onload

0
投票

您的第二个代码段无法正常工作,因为您在将页面发送到浏览器之前重定向响应。您的第一个代码段应该可以工作,但是,您需要在使用客户端脚本调用alert之后立即添加重定向逻辑。您可以使用要将用户重定向到的URL设置window.location属性

protected void Page_PreRender(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey", 
    "alert('my message');window.location='YOUR_URL_HERE';", true);
}

0
投票

你说你已经尝试过没有快乐的RegisterClientScriptBlock。我在页面加载事件中使用它来在我的页面上设置一堆用户权限,没有任何问题。您的前端是否有脚本管理器?

<asp:ScriptManager ID="ScriptManager1" runat="server" />

它应该在你打开<form>标签之后直接放置,然后你的页面中的服务器端加载它就像这样...

ClientScript.RegisterClientScriptBlock(this.GetType(), "callJs", "yourJsfunction();", true);
© www.soinside.com 2019 - 2024. All rights reserved.