简体   繁体   English

我如何导致回发?

[英]How do I cause a Postback?

I need to cause a postback in c#, how can i do this?我需要在 c# 中进行回发,我该怎么做? (It can not be through a button or any other element) Just want to cause a postback if a condition is met. (不能通过按钮或任何其他元素)只想在满足条件时进行回发。

something like就像是

  If(so and so)
      Postback now!
  else
      Do not post back

Your question does not make sense.你的问题没有意义。

C# code runs on the server, in response to a postback. C# 代码在服务器上运行,以响应回发。
Until a postback happens, no code can run.在回发发生之前,没有代码可以运行。

You may want to trigger a postback in Javascript , which runs in the browser.您可能希望在浏览器中运行Javascript触发回发

From the comments it looks like you are using telerik's RadTabs.从评论看来,您正在使用 Telerik 的 RadTabs。 You could potentially set AutoPostBack to true on the tab control so that it would force a refresh whenever the user switches tabs.您可以在选项卡控件上将 AutoPostBack 设置为 true,以便在用户切换选项卡时强制刷新。

http://www.telerik.com/help/aspnet/tabstrip/tab_server-side%20events.html http://www.telerik.com/help/aspnet/tabstrip/tab_server-side%20events.html

Some didactic contextualization: whatever piece of code you are trying to call in your "second postback", just call it in your "first postback" already!一些教学语境化:无论您试图在“第二次回发”中调用什么代码,只要在“第一次回发”中调用它即可!

Example:例子:

You have a method you want to call, say, a Button_Click in your "second postback"?你有一个你想在“第二次回发”中调用Button_Click的方法吗? Just call it in you "first postback":只需将其称为“第一次回发”即可:

btnSaveClick(btnSave, null);

You can always do a Response.Redirect(Request.RawUrl);你总是可以做一个Response.Redirect(Request.RawUrl); . .

You'll want to be careful that you don't cause an endless redirect loop.您需要小心不要导致无休止的重定向循环。

To force a postback in Server code, use ClientScript.GetPostBackEventReference() to generate the javascript code that does the postback.要在服务器代码中强制回发,请使用 ClientScript.GetPostBackEventReference() 生成执行回发的 javascript 代码。 Then add a startup script in your event handler that calls the postback javascript.然后在您的事件处理程序中添加一个启动脚本,该脚本调用回发 javascript。

aspx: aspx:

// Do an empty postback that doesn't do anything.
function doPostback() {
  <%= ClientScript.GetPostBackEventReference(btnDoPostback, String.Empty) %>;
}

<asp:Button ID="btnDoPostback" runat="server" OnClick="btnDoPostback_Click" Style="display: none;" />

cs: CS:

if (!IsPostBack) {
  ScriptManager.RegisterStartupScript(this, this.GetType(), "dopostback", "doPostback();", true);
}

protected void btnDoPostback_Click(object sender, EventArgs e)
{
}

You can't do a postback in your code-behind.您不能在代码隐藏中进行回发。 Your code-behind code IS your postback code.您的代码隐藏代码就是您的回发代码。 Why are you trying to do this?你为什么要这样做? Maybe we can help you in your program logic也许我们可以在您的程序逻辑方面为您提供帮助

Did you want to call the page again?您想再次调用该页面吗? You can do this with a Response.Redirect您可以使用Response.Redirect执行此操作

Do you mean on your.aspx/cshtml pages?你的意思是在 your.aspx/cshtml 页面上? If so use jquery's $.post如果是这样,请使用jquery 的$.post

If you really are in a controller (and you are already running in response to a postback) then you should probably refactor your code so that whatever logic you want to call (via your 'second post' back) can be called from your existing location as well as your other Post action.如果您确实在 controller 中(并且您已经在运行以响应回发),那么您可能应该重构您的代码,以便可以从您现有的位置调用您想要调用的任何逻辑(通过您的“第二个帖子”返回)以及您的其他 Post 操作。

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

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