使用 JavaScript 的 ASP.NET 回发

发布于 2024-08-03 00:26:42 字数 227 浏览 2 评论 0原文

我有几个小的 div 正在利用 jQuery 可拖动。这些 div 被放置在 UpdatePanel 中,在 Dragstop 上我使用 _doPostBack() JavaScript 函数,在其中我从页面的形式。

我的问题是,当我调用此函数时,整个页面都会重新加载,但我只想重新加载更新面板。

I have several small divs which are utilizing jQuery draggable. These divs are placed in an UpdatePanel, and on dragstop I use the _doPostBack() JavaScript function, where I extract necessary information from the page's form.

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.

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

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

发布评论

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

评论(8

故笙诉离歌 2024-08-10 00:26:42

这是一个完整的解决方案

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>

页面代码隐藏类的全部内容

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
  • 包含 LinkBut​​ton 是为了确保__doPostBack javascript 函数呈现给客户端。仅拥有 Button 控件不会导致呈现此 __doPostBack 函数。该功能将凭借大多数 ASP.NET 页面上具有的各种控件来呈现,因此通常不需要空链接按钮这是

怎么回事?

向客户端呈现两个输入控件:

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

__doPostBack 函数的呈现如下:

function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
  • 如您所见,它将值分配给隐藏输入。

当表单提交/发生回发时:

  • 如果您提供了要运行其按钮单击处理程序的服务器控制按钮的 UniqueID (javascript:__doPostBack('ButtonB','' ),然后该按钮的按钮单击处理程序将运行。

如果我不想运行单击处理程序,但想做其他事情,

您可以将任何您想要的内容作为参数传递给 。 >__doPostBack

然后,您可以分析隐藏的输入值并相应地运行特定代码:

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

其他注释

  • 如果我不知道要运行其单击处理程序的控件的 ID 怎么办?
    • 如果设置 ClientIDMode="Static" 不可接受,则可以执行以下操作:__doPostBack('<%= myclientid.UniqueID %>', ' ')
    • 或者:__doPostBack('<%= MYBUTTON.UniqueID %>','')
    • 如果您愿意,这会将控件的唯一 ID 注入到 JavaScript 中

Here is a complete solution

Entire form tag of the asp.net page

<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. Simply having Button controls will not cause this __doPostBack function to be rendered. 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

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
  • __EVENTARGUMENT receives argument 2 of __doPostBack

The __doPostBack function is rendered out like this:

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.

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

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?
    • If it is not acceptable to set ClientIDMode="Static", then you can do something like this: __doPostBack('<%= myclientid.UniqueID %>', '').
    • Or: __doPostBack('<%= MYBUTTON.UniqueID %>','')
    • This will inject the unique id of the control into the javascript, should you wish it
伊面 2024-08-10 00:26:42

Per Phairoh:在页面/组件中使用它,以防面板名称更改

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

Per Phairoh: Use this in the Page/Component just in case the panel name changes

<script type="text/javascript">
     <!--
     //must be global to be called by ExternalInterface
         function JSFunction() {
             __doPostBack('<%= myUpdatePanel.ClientID  %>', '');
         }
     -->
     </script>
胡渣熟男 2024-08-10 00:26:42

直接使用 __doPostBack 已经是 2000 年代的事情了。 2018 年编写 WebForms 的任何人都使用 GetPostBackEventReference

(更严重的是,添加此作为完整性的答案。直接使用 __doPostBack 是不好的做法(单下划线前缀通常表示私有成员,双下划线前缀表示更通用的私有成员)成员),尽管目前它可能不会改变或过时,我们在 ClientScriptManager.GetPostBackEventReference。)

假设您的 btnRefresh 位于我们的 UpdatePanel 内部并导致回发,您可以像这样使用 GetPostBackEventReference (灵感):

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

Using __doPostBack directly is sooooo the 2000s. Anybody coding WebForms in 2018 uses 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.)

Assuming your btnRefresh is inside our UpdatePanel and causes a postback, you can use GetPostBackEventReference like this (inspiration):

function RefreshGrid() {
    <%= ClientScript.GetPostBackEventReference(btnRefresh, String.Empty) %>;
}
甜警司 2024-08-10 00:26:42

虽然 Phairoh 的解决方案在理论上似乎是合理的,但我还找到了解决此问题的另一种解决方案。通过将 UpdatePanels id 作为 doPostBack 函数的参数(事件目标)传递,更新面板将回发,但不是整个页面。

__doPostBack('myUpdatePanelId','')

*注意:第二个参数用于添加事件参数希望

这对某人有帮助!

编辑:所以看来我在打字时上面也给出了同样的建议:)

While Phairoh's solution seems theoretically sound, I have also found another solution to this problem. 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.

__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 :)

兔小萌 2024-08-10 00:26:42

如果有人遇到此问题(就像我一样),您可以通过向按钮添加 UseSubmitBehavior="false" 属性来获取按钮的回发代码。如果您检查按钮的渲染源,您将看到您需要执行的确切 JavaScript。就我而言,它使用按钮的名称而不是 id。

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. If you examine the rendered source of the button, you'll see the exact javascript you need to execute. In my case it was using the name of the button rather than the id.

夢归不見 2024-08-10 00:26:42

您是否尝试过将更新面板的客户端 ID 传递给 __doPostBack 函数?我的团队这样做是为了刷新更新面板,据我所知它有效。

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

Have you tried passing the Update panel's client id to the __doPostBack function? My team has done this to refresh an update panel and as far as I know it worked.

__doPostBack(UpdatePanelClientID, '**Some String**');
傲性难收 2024-08-10 00:26:42

首先,不要使用更新面板。它们是微软为网络开发者创造的第二个最邪恶的东西。

其次,如果您必须使用更新面板,请尝试将UpdateMode 属性设置为Conditional。然后向添加到页面的 Asp:Hidden 控件添加触发器。将更改事件指定为触发器。在 Dragstop 事件中,更改隐藏控件的值。

这是未经测试的,但理论似乎是合理的...如果这不起作用,您可以使用 asp:button 尝试相同的操作,只需在其上设置 display:none 样式并使用 click 事件而不是更改事件。

First, don't use update panels. They are the second most evil thing that Microsoft has ever created for the web developer.

Second, if you must use update panels, try setting the UpdateMode property to Conditional. Then add a trigger to an Asp:Hidden control that you add to the page. Assign the change event as the trigger. In your dragstop event, change the value of the hidden control.

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.

喜爱纠缠 2024-08-10 00:26:42

您无法调用 _doPostBack(),因为它会强制提交表单。为什么不在 UpdatePanel 上禁用 PostBack 呢?

You can't call _doPostBack() because it forces submition of the form. Why don't you disable the PostBack on the UpdatePanel?

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