从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",但它没有用。 谢谢先进
答案 0 :(得分:0)
如果你想用javascript检查页面加载的东西,那么使用
$(document).ready(function () {
//Your code here
} );
或在javascript中
window.onload
答案 1 :(得分:0)
您的第二个代码段无效,因为您在将页面发送到浏览器之前重定向响应。您的第一个代码段应该可以使用,但是,您需要在使用客户端脚本调用alert
之后立即添加重定向逻辑。您可以使用要将用户重定向到
window.location
属性
protected void Page_PreRender(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey",
"alert('my message');window.location='YOUR_URL_HERE';", true);
}
答案 2 :(得分:0)
你说你已经尝试RegisterClientScriptBlock
而没有快乐。我在页面加载事件中使用它来在我的页面上设置一堆用户权限,没有任何问题。您的前端是否有脚本管理器?
<asp:ScriptManager ID="ScriptManager1" runat="server" />
应该在您打开<form>
标记后直接放置,然后在您的页面中加载调用它就像这样......
ClientScript.RegisterClientScriptBlock(this.GetType(), "callJs", "yourJsfunction();", true);