简体   繁体   English

使用 JavaScript 的 ASP.NET 回发

[英]ASP.NET postback with JavaScript

I have several small div s which are utilizing jQuery draggable.我有几个使用jQuery可拖动的小div These div s are placed in an UpdatePanel , and on dragstop I use the _doPostBack() JavaScript function, where I extract necessary information from the page's form.这些div放置在UpdatePanel ,在 dragstop 上我使用_doPostBack() JavaScript 函数,在那里我从页面的表单中提取必要的信息。

My problem is that when I call this function, the whole page is re-loaded, but I only want the update panel to be re-loaded.我的问题是,当我调用这个函数时,整个页面都会重新加载,但我只想重新加载更新面板。

Here is a complete solution这是一个完整的解决方案

Entire form tag of the asp.net page asp.net 页面的整个表单标签

<form id="form1" runat="server">
    <asp:LinkButton ID="LinkButton1" runat="server" /> <%-- included to force __doPostBack javascript function to be rendered --%>

    <input type="button" id="Button45" name="Button45" onclick="javascript:__doPostBack('ButtonA','')" value="clicking this will run ButtonA.Click Event Handler" /><br /><br />
    <input type="button" id="Button46" name="Button46" onclick="javascript:__doPostBack('ButtonB','')" value="clicking this will run ButtonB.Click Event Handler" /><br /><br />

    <asp:Button runat="server" ID="ButtonA" ClientIDMode="Static" Text="ButtonA" /><br /><br />
    <asp:Button runat="server" ID="ButtonB" ClientIDMode="Static" Text="ButtonB" />
</form>

Entire Contents of the Page's Code-Behind Class页面代码隐藏类的全部内容

Private Sub ButtonA_Click(sender As Object, e As System.EventArgs) Handles ButtonA.Click
    Response.Write("You ran the ButtonA click event")
End Sub

Private Sub ButtonB_Click(sender As Object, e As System.EventArgs) Handles ButtonB.Click
    Response.Write("You ran the ButtonB click event")
End Sub
  • The LinkButton is included to ensure that the __doPostBack javascript function is rendered to the client.包含 LinkBut​​ton 以确保 __doPostBack javascript 函数呈现给客户端。 Simply having Button controls will not cause this __doPostBack function to be rendered.仅具有 Button 控件不会导致呈现此 __doPostBack 函数。 This function will be rendered by virtue of having a variety of controls on most ASP.NET pages, so an empty link button is typically not needed由于在大多数 ASP.NET 页面上具有多种控件,因此将呈现此功能,因此通常不需要空链接按钮

What's going on?这是怎么回事?

Two input controls are rendered to the client:向客户端呈现两个输入控件:

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
  • __EVENTTARGET receives argument 1 of __doPostBack __EVENTTARGET接收 __doPostBack 的参数 1
  • __EVENTARGUMENT receives argument 2 of __doPostBack __EVENTARGUMENT接收 __doPostBack 的参数 2

The __doPostBack function is rendered out like this: __doPostBack 函数呈现如下:

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
  • As you can see, it assigns the values to the hidden inputs.如您所见,它将值分配给隐藏的输入。

When the form submits / postback occurs:当表单提交/回发时:

  • If you provided the UniqueID of the Server-Control Button whose button-click-handler you want to run ( javascript:__doPostBack('ButtonB','') , then the button click handler for that button will be run.如果您提供了要运行其按钮单击处理程序的服务器控制按钮的 UniqueID ( javascript:__doPostBack('ButtonB','') ,则将运行该按钮的按钮单击处理程序。

What if I don't want to run a click handler, but want to do something else instead?如果我不想运行点击处理程序,而是想做其他事情怎么办?

You can pass whatever you want as arguments to __doPostBack您可以将任何您想要的参数作为参数传递给__doPostBack

You can then analyze the hidden input values and run specific code accordingly:然后,您可以分析隐藏的输入值并相应地运行特定代码:

If Request.Form("__EVENTTARGET") = "DoSomethingElse" Then
    Response.Write("Do Something else") 
End If

Other Notes其他注意事项

  • What if I don't know the ID of the control whose click handler I want to run?如果我不知道要运行其单击处理程序的控件的 ID,该怎么办?
    • If it is not acceptable to set ClientIDMode="Static" , then you can do something like this: __doPostBack('<%= myclientid.UniqueID %>', '') .如果设置ClientIDMode="Static"是不可接受的,那么您可以执行以下操作: __doPostBack('<%= myclientid.UniqueID %>', '')
    • Or: __doPostBack('<%= MYBUTTON.UniqueID %>','')或者: __doPostBack('<%= MYBUTTON.UniqueID %>','')
    • This will inject the unique id of the control into the javascript, should you wish it如果您愿意,这会将控件的唯一 ID 注入到 javascript 中

Per Phairoh: Use this in the Page/Component just in case the panel name changes Per Phairoh:在页面/组件中使用它,以防面板名称更改

<script type="text/javascript">
     <!--
     //must be global to be called by ExternalInterface
         function JSFunction() {
             __doPostBack('<%= myUpdatePanel.ClientID  %>', '');
         }
     -->
     </script>

While Phairoh's solution seems theoretically sound, I have also found another solution to this problem.虽然 Phairoh 的解决方案在理论上似乎是合理的,但我也找到了另一个解决此问题的方法。 By passing the UpdatePanels id as a paramater (event target) for the doPostBack function the update panel will post back but not the entire page.通过将 UpdatePanels id 作为参数(事件目标)传递给 doPostBack 函数,更新面板将回发,但不会回发整个页面。

__doPostBack('myUpdatePanelId','')

*note: second parameter is for addition event args *注意:第二个参数用于添加事件参数

hope this helps someone!希望这有助于某人!

EDIT: so it seems this same piece of advice was given above as i was typing :)编辑:所以似乎在我打字时上面给出了同样的建议:)

Using __doPostBack directly is sooooo the 2000s.直接使用__doPostBack太棒了 2000 年代。 Anybody coding WebForms in 2018 uses GetPostBackEventReference任何在 2018 年编写 WebForms 的人都使用 GetPostBackEventReference

(More seriously though, adding this as an answer for completeness. Using the __doPostBack directly is bad practice (single underscore prefix typically indicates a private member and double indicates a more universal private member), though it probably won't change or become obsolete at this point. We have a fully supported mechanism in ClientScriptManager.GetPostBackEventReference .) (更严重的是,将其添加为完整性的答案。直接使用__doPostBack是不好的做法(单下划线前缀通常表示私有成员,双下划线表示更通用的私有成员),尽管它可能不会改变或在这一点。我们在ClientScriptManager.GetPostBackEventReference 中有一个完全支持的机制。)

Assuming your btnRefresh is inside our UpdatePanel and causes a postback, you can use GetPostBackEventReference like this ( inspiration ):假设您的 btnRefresh 位于我们的 UpdatePanel 内并导致回发,您可以像这样使用 GetPostBackEventReference(灵感):

function RefreshGrid() {
    <%= ClientScript.GetPostBackEventReference(btnRefresh, String.Empty) %>;
}

If anyone's having trouble with this (as I was), you can get the postback code for a button by adding the UseSubmitBehavior="false" attribute to it.如果有人对此有问题(就像我一样),您可以通过向按钮添加 UseSubmitBehavior="false" 属性来获取按钮的回发代码。 If you examine the rendered source of the button, you'll see the exact javascript you need to execute.如果您检查按钮的渲染源,您将看到您需要执行的确切 javascript。 In my case it was using the name of the button rather than the id.在我的情况下,它使用按钮的名称而不是 id。

Have you tried passing the Update panel's client id to the __doPostBack function?您是否尝试过将更新面板的客户端 ID 传递给 __doPostBack 函数? My team has done this to refresh an update panel and as far as I know it worked.我的团队这样做是为了刷新更新面板,据我所知它有效。

__doPostBack(UpdatePanelClientID, '**Some String**');

You can't call _doPostBack() because it forces submition of the form.您不能调用_doPostBack()因为它会强制提交表单。 Why don't you disable the PostBack on the UpdatePanel ?为什么不在UpdatePanel上禁用PostBack

First, don't use update panels.首先,不要使用更新面板。 They are the second most evil thing that Microsoft has ever created for the web developer.它们是 Microsoft 为 Web 开发人员创建的第二个最邪恶的东西。

Second, if you must use update panels, try setting the UpdateMode property to Conditional.其次,如果您必须使用更新面板,请尝试将 UpdateMode 属性设置为 Conditional。 Then add a trigger to an Asp:Hidden control that you add to the page.然后向添加到页面的 Asp:Hidden 控件添加触发器。 Assign the change event as the trigger.将更改事件指定为触发器。 In your dragstop event, change the value of the hidden control.在您的 dragstop 事件中,更改隐藏控件的值。

This is untested, but the theory seems sound... If this does not work, you could try the same thing with an asp:button, just set the display:none style on it and use the click event instead of the change event.这是未经测试的,但理论似乎是合理的...如果这不起作用,您可以使用 asp:button 尝试相同的操作,只需在其上设置 display:none 样式并使用 click 事件而不是 change 事件。

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

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