Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 回发导致页面第一次加载_C#_Asp.net_Postback - Fatal编程技术网

C# 回发导致页面第一次加载

C# 回发导致页面第一次加载,c#,asp.net,postback,C#,Asp.net,Postback,我正在使用: Response.Redirect(Request.RawUrl); 在我的网页上强制回发。但是,代码中的执行路径就好像是第一次加载页面一样。我怎样才能引起普通的回邮呢 任何建议都将不胜感激 问候 编辑: 这篇文章似乎吸引了很多负面因素。我是在谷歌搜索了一个发回的后,从一个股票溢出帖子中得到这个方法的。因此,下层选民可能应该在那篇文章上表达他们的不满,而不是我的 为了澄清对我的问题的一些误解: 我有一个事件按钮,它在方法体中包含逻辑——显然。但是,代码的执行流程如下所示: pro

我正在使用:

Response.Redirect(Request.RawUrl);
在我的网页上强制回发。但是,代码中的执行路径就好像是第一次加载页面一样。我怎样才能引起普通的回邮呢

任何建议都将不胜感激

问候

编辑:

这篇文章似乎吸引了很多负面因素。我是在谷歌搜索了一个发回的后,从一个股票溢出帖子中得到这个方法的。因此,下层选民可能应该在那篇文章上表达他们的不满,而不是我的

为了澄清对我的问题的一些误解:

我有一个事件按钮,它在方法体中包含逻辑——显然。但是,代码的执行流程如下所示:

protected void RedoEdits_Click(Object sender, EventArgs e)
{
    // Goes to Page_Load here with IsPostBack == true
    string trendsFileLocation = currDir + "\\" + reportDir + "\\" + trendsFile;
    string messagesFileLocation = currDir + "\\" + reportDir + "\\" + messagesFile;
    if (File.Exists(trendsFileLocation))
    {
        File.Delete(trendsFileLocation);
    }
    if (File.Exists(messagesFileLocation))
    {
        File.Delete(messagesFileLocation);
    }
    trendsXML.Clear();

    ViewReport_Click(null, null);
}
按钮单击-->页面加载(IsPostBack==true)-->按钮单击正文-->执行结束

所以我的问题是,Button\u Click主体中的逻辑没有显示在页面上,因为回发是在Button\u Click主体中的任何代码执行之前完成的

编辑:

活动内容如下:

protected void RedoEdits_Click(Object sender, EventArgs e)
{
    // Goes to Page_Load here with IsPostBack == true
    string trendsFileLocation = currDir + "\\" + reportDir + "\\" + trendsFile;
    string messagesFileLocation = currDir + "\\" + reportDir + "\\" + messagesFile;
    if (File.Exists(trendsFileLocation))
    {
        File.Delete(trendsFileLocation);
    }
    if (File.Exists(messagesFileLocation))
    {
        File.Delete(messagesFileLocation);
    }
    trendsXML.Clear();
    editMode = "redo";
}
IsPostBack==true的代码段:

if (editMode.Equals("redo"))
{
    editMode = "";
    ViewReport_Click(null, null);
}
代码段永远不会被执行,因为回发首先发生,而跳过代码段,因为editMode!=redo,然后以RedoEdits\u Click的主体完成,该主体最终设置editMode==redo,但是由于回发已经发生,我们无法访问ViewReport\u Click


顺便说一句,editMode是一个会话变量,因此它会持续存在。

Response.Redirect()不会导致任何回发,因为它只向服务器发送一个简单的GET请求。

Response.Redirect()不会导致任何回发,因为它只向服务器发送一个简单的GET请求。

这是因为重定向确实会像第一次一样加载页面。它根本没有发回。这可能在一个事件处理程序中,比如按钮单击,但一旦在该回发事件处理程序中重定向,它就不再在同一实例的回发中,您将从头开始重新加载页面

范例

private void MyButton_Click(Object sender, EventArgs e)
{
   lblStatus.Text = "Button Clicked!";  // still within a postback
   Response.Redirect(Request.RawUrl);  // now leaving the postback.  At this point, exexution stops and the page is loaded from scratch, as if we're not in a postback.

   lblStatus.Text = "Done posting back";  // This code will never execute.,  Response.Redirect throws a System.ThreadAbortException, and by default, code execution stops once Response.Redirect happens.

}
如果希望触发真正的回发而不从头开始加载页面,请在屏幕上放置一个按钮或一些控件,并使用该按钮或控件触发真正的回发,而不要调用Response.Redirect()


由于以下评论而添加:

让你自己熟悉这本书。如果在页面加载中发生了您不希望在回发中发生的事情,那么您应该用

if(!Page.IsPostback)
{
  // stuff that should only happen on initial load here.
}

或者考虑在不同的事件中做这些事情。例如,如果要在飞行中创建控件,则应该在Page_Init中完成,因为它发生在应用ViewState之前。如果这样做,每次回发时都会在Page_Init中创建控件,然后从Viewstate应用值,然后到达Page_Load,在那里可以使用它们。

这是因为重定向确实会像第一次加载页面一样加载页面。它根本没有发回。这可能在一个事件处理程序中,比如按钮单击,但一旦在该回发事件处理程序中重定向,它就不再在同一实例的回发中,您将从头开始重新加载页面

范例

private void MyButton_Click(Object sender, EventArgs e)
{
   lblStatus.Text = "Button Clicked!";  // still within a postback
   Response.Redirect(Request.RawUrl);  // now leaving the postback.  At this point, exexution stops and the page is loaded from scratch, as if we're not in a postback.

   lblStatus.Text = "Done posting back";  // This code will never execute.,  Response.Redirect throws a System.ThreadAbortException, and by default, code execution stops once Response.Redirect happens.

}
如果希望触发真正的回发而不从头开始加载页面,请在屏幕上放置一个按钮或一些控件,并使用该按钮或控件触发真正的回发,而不要调用Response.Redirect()


由于以下评论而添加:

让你自己熟悉这本书。如果在页面加载中发生了您不希望在回发中发生的事情,那么您应该用

if(!Page.IsPostback)
{
  // stuff that should only happen on initial load here.
}

或者考虑在不同的事件中做这些事情。例如,如果要在飞行中创建控件,则应该在Page_Init中完成,因为它发生在应用ViewState之前。如果这样做,控件将在每次回发时在Page_Init中创建,然后从Viewstate应用值,然后到达Page_Load,您可以在那里使用它们。

您不能强制从代码后面回发。你会怎么做?例如,单击按钮时会发生回发。即使有可能从代码背后强制它(当然有办法),你会通过这种方式实现什么?只要调用你想要的方法,不要做这样愚蠢的事情

回发=将数据/请求回发到同一页面。重定向显然不起作用

编辑基于OPs编辑:

您需要重新考虑您的工作流程,因此可以这样做:

protected void RedoEdits_Click(Object sender, EventArgs e)
{
    // Goes to Page_Load here with IsPostBack == true
    string trendsFileLocation = currDir + "\\" + reportDir + "\\" + trendsFile;
    string messagesFileLocation = currDir + "\\" + reportDir + "\\" + messagesFile;
    if (File.Exists(trendsFileLocation))
    {
        File.Delete(trendsFileLocation);
    }
    if (File.Exists(messagesFileLocation))
    {
        File.Delete(messagesFileLocation);
    }
    trendsXML.Clear();

    ViewReport_Click(null, null);
}
尝试模仿回发确实不是一个好主意


或者,可能是更好的方法-定义一个新事件,比如EditModeActive(或您想要的任何名称),它将在每次分配editMode=“redo”时触发。然后只需订阅此活动并执行任何您想要的操作。

您不能强制从代码隐藏处回发。你会怎么做?例如,单击按钮时会发生回发。即使有可能从代码背后强制它(当然有办法),你会通过这种方式实现什么?只要调用你想要的方法,不要做这样愚蠢的事情

回发=将数据/请求回发到同一页面。重定向显然不起作用

编辑基于OPs编辑:

您需要重新考虑您的工作流程,因此可以这样做:

protected void RedoEdits_Click(Object sender, EventArgs e)
{
    // Goes to Page_Load here with IsPostBack == true
    string trendsFileLocation = currDir + "\\" + reportDir + "\\" + trendsFile;
    string messagesFileLocation = currDir + "\\" + reportDir + "\\" + messagesFile;
    if (File.Exists(trendsFileLocation))
    {
        File.Delete(trendsFileLocation);
    }
    if (File.Exists(messagesFileLocation))
    {
        File.Delete(messagesFileLocation);
    }
    trendsXML.Clear();

    ViewReport_Click(null, null);
}
尝试模仿回发确实不是一个好主意


或者,可能是更好的方法-定义一个新事件,比如EditModeActive(或您想要的任何名称),它将在每次分配editMode=“redo”时触发。然后只需订阅此活动并执行任何您想要的操作。

我需要重新加载页面以恢复我原始的未编辑HTML。编辑表格的过程替换了一些InnerHTML,我需要重新开始进行编辑。因此,我只是移动了重新编辑iPostBack代码所需的逻辑!=true并保留Response.Redirect(Request.RawUrl)。工作起来很有魅力。

我需要它