C# IsPostBack 总是返回 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/912888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:40:21  来源:igfitidea点击:

IsPostBack always returns false

c#asp.nethtml

提问by

Every time I test the IsPostBack in PageLoad() false is returned whether or not post data is present. My first reaction was to check to see if the runat="server" tag was missing from the form or submit button. However, they were all added and the WriteEmail.aspx page still always returns false for IsPostBack. I have also tried using IsCrossPagePostBack in place of IsPostBack.

每次我在 PageLoad() 中测试 IsPostBack 时,无论发布数据是否存在,都会返回 false。我的第一反应是检查表单或提交按钮中是否缺少 runat="server" 标记。但是,它们都已添加,并且 WriteEmail.aspx 页面仍然始终为 IsPostBack 返回 false。我也尝试过使用 IsCrossPagePostBack 代替 IsPostBack。

ListInstructors.aspx:

ListInstructors.aspx:

<form runat="server" method="post" action="WriteEmail.aspx">
      ...
      <input type="submit" id="writeEmail" value="Write Email" runat="server" />
</form>

WriteEmail.aspx:

写电子邮件.aspx:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Response.Redirect("ListInstructors.aspx");
    }
}

采纳答案by Joel Coehoorn

Post != Postback.A postback is when you post back to the samepage. The action on your form is posting to a newpage.

发布 != 回传。回发是指您回发到同一页面。表单上的操作是发布到页面。

It looks like all you're doing is using the WriteEmail.aspx page to send a message and then going back to where you just were. You're not even displaying a form to collect the text there. It's a very... Classic ASP-ish... way to handle things.

看起来您所做的只是使用 WriteEmail.aspx 页面发送消息,然后返回到您刚才所在的位置。你甚至没有显示一个表单来收集那里的文本。这是一种非常……经典的 ASP 风格……处理事情的方式。

Instead, put the code you use to send a message in class separate class and if needed put the class in the App_Code folder. Also change the submit button to an <asp:button ... />Then you can just call it the code from the server's Click event for your button and never leave your ListInstructors.aspxpage.

相反,将用于发送消息的代码放在类单独的类中,如果需要,将该类放在 App_Code 文件夹中。还将提交按钮更改为<asp:button ... />然后您可以将服务器的 Click 事件中的代码调用为您的按钮,并且永远不要离开您的ListInstructors.aspx页面。



In response to your comment: No. From MSDN:

回应您的评论:不。来自MSDN

... make a cross-page request by assigning a page URL to the PostBackUrl property of a button control that implements the IButtonControl interface.

...通过将页面 URL 分配给实现 IButtonControl 接口的按钮控件的 PostBackUrl 属性来发出跨页面请求。

回答by Paul Sonier

The IsPostBackis not true because the form is not being submitted from the WriteEmail.aspxpage; submitting a form from the same page is what causes a PostBack. If you submitted the form from the WriteEmail.aspxpage, it would be a PostBack; as it is, it's just a Post.

IsPostBack因为形式没有被从提交的是不正确的WriteEmail.aspx页面; 从同一页面提交表单是导致PostBack. 如果您从WriteEmail.aspx页面提交表单,它将是一个PostBack; 事实上,它只是一个帖子。

You might find this MSDN reference to be useful:

您可能会发现此 MSDN 参考很有用:

http://msdn.microsoft.com/en-us/library/ms178141.aspx

http://msdn.microsoft.com/en-us/library/ms178141.aspx