显示列表

public class BasePage : System.Web.UI.Page
{
    public BasePage () {}
}

public class Test : BasePage
{
    public Test()
    {
        MessageBox.Alert(text);

        // 下里还会被执行
        // MessageBox.Alert 中的 System.Web.HttpContext.Current.Response.End(); 不起作用
        // Response.End() 在构造函数中不起作用
        System.IO.File.WriteAllText("C:/1.txt", "1");
    }
}

public class MessageBox
{
    public static void Alert(string text)
    {
        System.Web.HttpContext.Current.Response.Write(text);
        System.Web.HttpContext.Current.Response.End();        
    }
}




解决方法
不要在构造函数中执行,在 Page_Load 中执行



public class Test : System.Web.UI.Page
{
    public Test()
    {
        // 停止了输出
        System.Web.HttpContext.Current.Response.End();

        // 下面的代码还会执行
        System.IO.File.WriteAllText("C:/1.txt", "Test");
    }
}

public class Test : System.Web.UI.Page
{
    private void Page_Load(object sender, System.EventArgs e)
    {
        System.Web.HttpContext.Current.Response.End();

        // 下面的代码不会执行
        System.IO.File.WriteAllText("C:/1.txt", "Page_Load");
    }
}

标签:错误 
返回摘要 | 分类(Asp.Net) | 访问(35) | 编辑