强制回发

发布于 2024-12-17 03:01:41 字数 73 浏览 4 评论 0原文

有没有办法在代码中强制回发?

我希望强制从我的 asp.net Web 应用程序背后的 C# 代码中的方法引发回发。

Is there a way to force a postback in code?

I'm looking to force the raising of a postback from a method in the c# code behind my asp.net web application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

惜醉颜 2024-12-24 03:01:41

您可以尝试重定向到同一页面。

Response.Redirect(Request.RawUrl);

You can try redirecting to same page.

Response.Redirect(Request.RawUrl);
半葬歌 2024-12-24 03:01:41

表单提交后会触发回发,因此它与客户端操作有关......
看一下这里的解释:
ASP.NET - 是否可以从服务器代码触发回发?

,这里有一个解决方案:
http://forums.asp.net/t/928411.aspx/1

A postback is triggered after a form submission, so it's related to a client action...
take a look here for an explanation:
ASP.NET - Is it possible to trigger a postback from server code?

and here for a solution:
http://forums.asp.net/t/928411.aspx/1

白云不回头 2024-12-24 03:01:41

更简单:

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "DoPostBack", "__doPostBack(sender, e)", true);

Simpler:

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "DoPostBack", "__doPostBack(sender, e)", true);
乖不如嘢 2024-12-24 03:01:41

这里的解决方案来自 http://forums.asp.net/t/928411.aspx/1 正如 mamoo 所提到的 - 以防网站离线。对我来说效果很好。

StringBuilder sbScript = new StringBuilder();

sbScript.Append("<script language='JavaScript' type='text/javascript'>\n");
sbScript.Append("<!--\n");
sbScript.Append(this.GetPostBackEventReference(this, "PBArg") + ";\n");
sbScript.Append("// -->\n");
sbScript.Append("</script>\n");

this.RegisterStartupScript("AutoPostBackScript", sbScript.ToString());

Here the solution from http://forums.asp.net/t/928411.aspx/1 as mentioned by mamoo - just in case the website goes offline. Worked well for me.

StringBuilder sbScript = new StringBuilder();

sbScript.Append("<script language='JavaScript' type='text/javascript'>\n");
sbScript.Append("<!--\n");
sbScript.Append(this.GetPostBackEventReference(this, "PBArg") + ";\n");
sbScript.Append("// -->\n");
sbScript.Append("</script>\n");

this.RegisterStartupScript("AutoPostBackScript", sbScript.ToString());
晨曦慕雪 2024-12-24 03:01:41

不,不是来自代码隐藏。回发是使用 Http POST 方法从客户端上的页面发起回服务器上自身的请求。在服务器端,您可以请求重定向,但将是 Http GET 请求。

No, not from code behind. A postback is a request initiated from a page on the client back to itself on the server using the Http POST method. On the server side you can request a redirect but the will be Http GET request.

原来是傀儡 2024-12-24 03:01:41

您可以使用 Repeater 或 ListView 等数据绑定控件,根据需要将其重新绑定到控件属性列表,并让它动态生成控件。

作为替代方案,您可以使用 Response.Redirect(".") 重新加载同一页面。

You can use a data-bound control like the Repeater or ListView, re-bind it to a list of control properties as needed, and let it generate the controls dynamically.

As an alternative, you can use Response.Redirect(".") to re-load the same page.

メ斷腸人バ 2024-12-24 03:01:41

通过使用 Server.Transfer("YourCurrentPage.aspx");我们可以轻松实现这一点,并且它比 Response.Redirect() 更好;因为 Server.Transfer() 将为您节省往返时间。

By using Server.Transfer("YourCurrentPage.aspx"); we can easily acheive this and it is better than Response.Redirect(); coz Server.Transfer() will save you the round trip.

清引 2024-12-24 03:01:41

您可以从 Page_Load 事件中手动调用 PostBack 调用的方法:

public void Page_Load(object sender, EventArgs e)
{
    MyPostBackMethod(sender, e);
}

但如果您的意思是可以将 Page.IsPostBack 属性设置为 true 如果没有真正的回帖,那么答案是否定的。

You can manually call the method invoked by PostBack from the Page_Load event:

public void Page_Load(object sender, EventArgs e)
{
    MyPostBackMethod(sender, e);
}

But if you mean if you can have the Page.IsPostBack property set to true without real post back, then the answer is no.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文