简体   繁体   English

Page_Load问题

[英]Page_Load question

I have this javascript modal which I want to use for error reporting. 我有这个JavaScript模式,我想用于错误报告。 Everything works well only that the code gets executed before page load. 只有在页面加载之前执行代码的情况下,一切工作正常。 So the result is that I have my javascript before the head tag which means nothing gets executed! 因此,结果是我的head标记之前有我的JavaScript,这意味着什么都不会执行!

Any help? 有什么帮助吗?

public void showError(string error)
{
    string script = "<script type=\"text/javascript\">$.facebox.settings.opacity = 0.4;jQuery.facebox(\""+error+"\");</script>";        
    System.Web.HttpContext.Current.Response.Write(script);        
}

By the way, my ShowError method is within a class called ErrorDisplay This is how I call it: 顺便说一句,我的ShowError方法在名为ErrorDisplay的类中,这就是我的称呼方式:

string strErr = "error here";
            ErrorDisplay myError = new ErrorDisplay();
            myError.ShowError(strErr);

The basic problem is that you need to make your Javascript wait for the DOM to be ready before it executes. 基本问题是您需要让Javascript等待DOM准备就绪后再执行。

The easiest way around this is to use the ClientScriptManager.RegisterStartupScript method to execute your code rather than add it to the page directly: 解决此问题的最简单方法是使用ClientScriptManager.RegisterStartupScript方法执行代码,而不是将其直接添加到页面中:

public void Page_Load(Object sender, EventArgs e)
{
    Type myType = this.GetType();
    string script = "<script>alert('Hello World! I am ready.');</script>";

    Page.ClientScript.RegisterStartupScript(myType, "StartupScript", script);
}

UPDATE 更新

My example was based on the fact that your original question was specifically about Page_Load. 我的示例基于以下事实:您最初的问题专门针对Page_Load。 The code only needs to be modified slightly to fit your example though: 但是,只需稍微修改一下代码即可适合您的示例:

public void ShowError(string error)
{
    StringBuilder sb = new StringBuilder();
    s.Append("<script type='text/javascript'>");
    s.Append("$.facebox.settings.opacity = 0.4;");
    s.AppendFormat("$.facebox('{0}');", error);
    s.Append("</script>");

    Type t = this.GetType();
    Page.ClientScript.RegisterStartupScript(t, "ErrorScript", sb.ToString());
}

Assuming your just looking to have the code in Page_Load called later try using the Page_PreRender method instead. 假设您只是想稍后调用Page_Load中的代码,请尝试改用Page_PreRender方法。 You can find a list of events and the order they are called located here http://msdn.microsoft.com/en-us/library/aa479007.aspx 您可以在以下位置找到事件列表及其调用顺序: http://msdn.microsoft.com/zh-cn/library/aa479007.aspx

This is how I achieved it: 这是我实现的方式:

public void ShowError(Page CurrentPage,string error)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("<script type='text/javascript'>");
    sb.Append("$.facebox.settings.opacity = 0.4;");
    sb.AppendFormat("jQuery.facebox('{0}');", error);
    sb.Append("</script>");

    Type t = this.GetType();

    CurrentPage.ClientScript.RegisterStartupScript(t, "ErrorScript", sb.ToString());
}

When I call it(this,"error string"); 当我调用它(这是“错误字符串”)时;

Thanks a lot guys!! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM