使用 JavaScript 的 ASP.NET 回发
我有几个小的 div
正在利用 jQuery
可拖动。这些 div
被放置在 UpdatePanel
中,在 Dragstop 上我使用 _doPostBack()
JavaScript 函数,在其中我从页面的形式。
我的问题是,当我调用此函数时,整个页面都会重新加载,但我只想重新加载更新面板。
I have several small div
s which are utilizing jQuery
draggable. 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是一个完整的解决方案
asp.net 页面的整个表单标记
页面代码隐藏类的全部内容
怎么回事?
向客户端呈现两个输入控件:
__EVENTTARGET
接收 __doPostBack 的参数 1__EVENTARGUMENT
接收 __doPostBack 的参数 2__doPostBack 函数的呈现如下:
当表单提交/发生回发时:
javascript:__doPostBack('ButtonB','' )
,然后该按钮的按钮单击处理程序将运行。如果我不想运行单击处理程序,但想做其他事情,
您可以将任何您想要的内容作为参数传递给
。 >__doPostBack
然后,您可以分析隐藏的输入值并相应地运行特定代码:
其他注释
ClientIDMode="Static"
不可接受,则可以执行以下操作:__doPostBack('<%= myclientid.UniqueID %>', ' ')
。__doPostBack('<%= MYBUTTON.UniqueID %>','')
Here is a complete solution
Entire form tag of the asp.net page
Entire Contents of the Page's Code-Behind Class
What's going on?
Two input controls are rendered to the client:
__EVENTTARGET
receives argument 1 of __doPostBack__EVENTARGUMENT
receives argument 2 of __doPostBackThe __doPostBack function is rendered out like this:
When the form submits / postback occurs:
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:
Other Notes
ClientIDMode="Static"
, then you can do something like this:__doPostBack('<%= myclientid.UniqueID %>', '')
.__doPostBack('<%= MYBUTTON.UniqueID %>','')
Per Phairoh:在页面/组件中使用它,以防面板名称更改
Per Phairoh: Use this in the Page/Component just in case the panel name changes
直接使用 __doPostBack 已经是 2000 年代的事情了。 2018 年编写 WebForms 的任何人都使用 GetPostBackEventReference
(更严重的是,添加此作为完整性的答案。直接使用
__doPostBack
是不好的做法(单下划线前缀通常表示私有成员,双下划线前缀表示更通用的私有成员)成员),尽管目前它可能不会改变或过时,我们在 ClientScriptManager.GetPostBackEventReference。)假设您的 btnRefresh 位于我们的 UpdatePanel 内部并导致回发,您可以像这样使用 GetPostBackEventReference (灵感):
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):
虽然 Phairoh 的解决方案在理论上似乎是合理的,但我还找到了解决此问题的另一种解决方案。通过将 UpdatePanels id 作为 doPostBack 函数的参数(事件目标)传递,更新面板将回发,但不是整个页面。
*注意:第二个参数用于添加事件参数希望
这对某人有帮助!
编辑:所以看来我在打字时上面也给出了同样的建议:)
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.
*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 :)
如果有人遇到此问题(就像我一样),您可以通过向按钮添加 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.
您是否尝试过将更新面板的客户端 ID 传递给 __doPostBack 函数?我的团队这样做是为了刷新更新面板,据我所知它有效。
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.
首先,不要使用更新面板。它们是微软为网络开发者创造的第二个最邪恶的东西。
其次,如果您必须使用更新面板,请尝试将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.
您无法调用
_doPostBack()
,因为它会强制提交表单。为什么不在UpdatePanel
上禁用PostBack
呢?You can't call
_doPostBack()
because it forces submition of the form. Why don't you disable thePostBack
on theUpdatePanel
?