C# 如何在asp.net中使用__doPostBack函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16361950/
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
how to use __doPostBack function in asp.net
提问by moe
i am trying to use __doPostBackfunction so i can force my page to do post pack on the page load but i am having some difficulties understanding.
我正在尝试使用__doPostBack功能,因此我可以强制我的页面在页面加载时进行发布包,但我在理解时遇到了一些困难。
when i was looking at the examples online. On button click, i want to do the post back but not sure how to complete the code in the code behind.
当我在网上看例子时。单击按钮时,我想回帖,但不确定如何完成后面代码中的代码。
Here is what i have so far:
这是我到目前为止所拥有的:
<script type="text/javascript">
function DoPostBack() {
__doPostBack('btnNew', 'MyArgument');
}
</script>
Here is my button
这是我的按钮
<asp:Button ID="btnNew" runat="server" CausesValidation="False" CommandName="New" OnClick="DoPostBack()" Text="Create" />
I don't seem to understand to use "MyArgument" in the code behind. What do i need to do in the code behind so it does post back on the page load? thanks in advance for the assistance.
我似乎不明白在后面的代码中使用“MyArgument”。我需要在后面的代码中做什么才能在页面加载时回发?提前感谢您的帮助。
回答by Andrei
The values that are passed to the function __doPostBackas arguments will be sent to the server as request parameters named __EVENTTARGETand __EVENTARGUMENT. ASP.NET uses these values to determine what control has fired an event and what arguments should be passed as EventArgsobject. But you can access them directly using object HttpRequest:
__doPostBack作为参数传递给函数的值将作为名为__EVENTTARGET和 的请求参数发送到服务器__EVENTARGUMENT。ASP.NET 使用这些值来确定哪个控件触发了事件以及哪些参数应该作为EventArgs对象传递。但是您可以使用 object 直接访问它们HttpRequest:
string eventTarget = this.Request.Params.Get("__EVENTTARGET");
string eventArgument = this.Request.Params.Get("__EVENTARGUMENT");
回答by Alex Filipovici
Scenario 1
场景一
If you want to use your JavaScriptfunction to trigger the postback, you need to replace the OnClickwith OnClientClick. Modify the button definition like this (I'm already assuming that it's nested inside an UpdatePanel):
如果你想用你的JavaScript功能触发回发,您需要更换OnClick用OnClientClick。像这样修改按钮定义(我已经假设它嵌套在 内UpdatePanel):
<asp:Button ID="btnNew"
runat="server"
CausesValidation="False"
CommandName="New"
OnClientClick="DoPostBack();"
Text="Create" />
In the code behind, in the Page_Loadmethod, read the Request["__EVENTTARGET"]and the Request["__EVENTARGUMENT"]:
在后面的代码中,在Page_Load方法中,读取Request["__EVENTTARGET"]和Request["__EVENTARGUMENT"]:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (Request["__EVENTTARGET"] == "btnNew" &&
Request["__EVENTARGUMENT"] == "MyArgument")
{
//do something
}
}
}
Scenario 2
场景二
If you don't necessarily want to use JavaScript, modify the button's definition like this:
如果您不一定要使用 JavaScript,请像这样修改按钮的定义:
<asp:Button ID="btnNew"
runat="server"
CausesValidation="False"
CommandName="New"
OnClick="DoPostback"
CommandArgument="MyArgument"
Text="Create" />
Then, add the following method in the code behind:
然后,在后面的代码中添加以下方法:
protected void DoPostback(object sender, EventArgs e)
{
var target = ((Button)(sender)).ClientID; //"btnNew"
var argument = ((Button)(sender)).CommandArgument; //"MyArgument"
if (target == "btnNew" &&
argument == "MyArgument")
{
//do something
}
}
回答by Rick
ASP.Net provides a way for you to build a call to perform a PostBack from javascript.
ASP.Net 为您提供了一种构建调用以从 javascript 执行 PostBack 的方法。
The ClientScriptManager.GetPostBackEventReference Method will build the code you need to use in javascript to perform a PostBack from a particular control http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getpostbackeventreference.aspx
ClientScriptManager.GetPostBackEventReference 方法将构建您需要在 javascript 中使用的代码以从特定控件执行回发 http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getpostbackeventreference.aspx
Below is an example (in VB.Net). In the code-behind, I'm creating my own javascript function. The ASP.Net code will insert javascript code in my function to perform the PostBack for the "MyButton" control. When the page is built, this javascript function will be available. When javascript code makes a call to this javascript function, it will then perform a PostBack, which will mimic the action that occurs when the "MyButton" button is clicked.
下面是一个示例(在 VB.Net 中)。在代码隐藏中,我正在创建自己的 javascript 函数。ASP.Net 代码将在我的函数中插入 javascript 代码以执行“MyButton”控件的回发。页面构建完成后,这个javascript函数就可用了。当 javascript 代码调用此 javascript 函数时,它将执行 PostBack,这将模拟单击“MyButton”按钮时发生的操作。
Dim csm As ClientScriptManager = Page.ClientScript
Dim sb As New StringBuilder
sb.AppendLine("function handleDoPostBack() {")
sb.AppendLine(csm.GetPostBackEventReference(MyButton, ""))
sb.AppendLine("}")
csm.RegisterClientScriptBlock(Me.Page.GetType, "js_code__create_functions", sb.ToString, True)
Hope that's what you're looking for.
希望这就是你要找的。
UPDATE
更新
Here's an example of how you might call the javascript function that was created for you on the server side.
下面是一个示例,说明如何调用在服务器端为您创建的 javascript 函数。
<input type="button" value="Click Me" onclick="handleDoPostBack();" />
I just made a regular HTML button (instead of using an ASP.Net button), because I didn't want to get the concept of a button PostBack confused with it. This is simply an HTML button that does nothing other than what you program it to do. Since I've added the javascript function call to the onclick, then when you press the button, it will perform the PostBack. When this PostBack occurs, it will act as though the "MyButton" button was clicked; that would be an ASP.Net button that you have somewhere on the page. Since that button is an ASP.Net button, it would already be doing a PostBack on its own, which is why I think there's some confusion about why you're looking to trigger a PostBack programatically.
我只是制作了一个普通的 HTML 按钮(而不是使用 ASP.Net 按钮),因为我不想将按钮 PostBack 的概念与它混淆。这只是一个 HTML 按钮,除了您对其进行编程之外什么都不做。由于我已将 javascript 函数调用添加到 onclick,因此当您按下按钮时,它将执行 PostBack。当这个 PostBack 发生时,它就像点击了“MyButton”按钮一样;那将是页面上某处的 ASP.Net 按钮。由于该按钮是一个 ASP.Net 按钮,它本身已经在执行回发,这就是为什么我认为您对为什么要以编程方式触发回发有些困惑。
You can also call the javascript function directly in code, as such...
您也可以直接在代码中调用 javascript 函数,因此...
<script>
handleDoPostBack();
</script>
This will occur as soon as the code encounters it. Or, if you put it in an onload event, then it would be called when the page was finished loading. But like I mentioned in my comment, you'd have to be careful with that because you may end up with an inifinite loop.
这将在代码遇到它时立即发生。或者,如果你把它放在一个 onload 事件中,那么它会在页面加载完成时被调用。但是就像我在评论中提到的那样,您必须小心谨慎,因为您最终可能会遇到无限循环。
From reading some of your comments in the other answers, the issue you're trying to resolve seems to have to do with the AJAX panels you're using. I'm not familar with those, so I'm not sure why it's not working for you, and I don't know if triggering the PostBack programatically is going to solve your issue.
通过阅读其他答案中的一些评论,您尝试解决的问题似乎与您使用的 AJAX 面板有关。我不熟悉这些,所以我不确定为什么它对你不起作用,我不知道以编程方式触发 PostBack 是否能解决你的问题。
However, if you do need to programatically trigger a PostBack, this is the code you could use. I use it in cases where I don't want a button to be on the page, but I want an action to take place. When something else happens on my page, then I run some javascript code that will act as though a button were clicked - and I can handle that button click on the server side. In my ASP.Net page, I do include the button, but then I use CSS to hide the user from seeing the button. So the user doesn't see the button, but my javascript code can call my javacript function which will mimic a click of that button.
但是,如果您确实需要以编程方式触发 PostBack,则可以使用此代码。我在不希望页面上有按钮但希望执行操作的情况下使用它。当我的页面上发生其他事情时,我会运行一些 javascript 代码,就像单击按钮一样 - 我可以在服务器端处理该按钮单击。在我的 ASP.Net 页面中,我确实包含了按钮,但随后我使用 CSS 来隐藏用户看不到该按钮。所以用户看不到按钮,但我的 javascript 代码可以调用我的 javacript 函数,该函数将模拟单击该按钮。
回答by Ram
below code works for me todo a postback using C#. Note i am not using the 'MyArgument' parameter and leaving it empty
下面的代码适用于我使用 C# 进行回发。请注意,我没有使用“MyArgument”参数并将其留空
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "DoPostBack", "__doPostBack('dummybtnPivotGridupdate', '')", true);
where
在哪里
<asp:Button ID="dummybtnPivotGridupdate" runat="server" Style="display: none;" ClientIDMode="Static" />
Hope this helps!
希望这可以帮助!

