使用ASP.NET AJAX从客户端控件将数据传递到服务器代码

时间:2022-01-04 14:53:02

I have a ListView on a page that displays a list of widgets. When a user clicks on one of the items in the list, I want to display a ModalPopup that contains controls allowing the user to operate on the item they selected.

我在页面上有一个ListView,它显示了一个小部件列表。当用户单击列表中的某个项目时,我想显示一个ModalPopup,其中包含允许用户操作所选项目的控件。

I could easily accomplish this by placing a Panel and a ModalPopupExtender in the ListView's ItemTemplate, but this mean one set of hidden controls for each and every widget, which would massively bloat the page size. (There are going to be some rather heavyweight controls in there.) Instead I want to reuse a single ModalPopup for each of the widgets in the list.

我可以通过在ListView的ItemTemplate中放置一个Panel和一个ModalPopupExtender来轻松实现这一点,但这意味着每个小部件都有一组隐藏的控件,这会大大增加页面大小。 (那里会有一些相当重量级的控件。)相反,我想为列表中的每个小部件重用一个ModalPopup。

I've done some searching but I haven't found anything that applies directly to my situation before. From what I've been able to figure out, however, I have to do something like this:

我做了一些搜索,但我还没有发现任何直接适用于我的情况。然而,从我能够弄清楚的,我必须做这样的事情:

  1. Place a Panel and a ModalPopupExtender on the page inside an UpdatePanel.
  2. 在UpdatePanel内的页面上放置一个Panel和一个ModalPopupExtender。

  3. Build a custom WidgetManipulator user control that has a WidgetID property. Put this in the Panel, along with a couple OK/Cancel buttons.
  4. 构建具有WidgetID属性的自定义WidgetManipulator用户控件。将其放在面板中,以及几个OK / Cancel按钮。

  5. In Javascript on the page, attach a click handler to each widget in the ListView that triggers a postback on the UpdatePanel.
  6. 在页面上的Javascript中,将单击处理程序附加到ListView中的每个窗口小部件,以触发UpdatePanel上的回发。

  7. On the UpdatePanel_Load event on the server, display the ModalPopup and then set the WidgetID propety on the WidgetManipulator to the ID of the clicked widget.
  8. 在服务器上的UpdatePanel_Load事件中,显示ModalPopup,然后将WidgetManipulator上的WidgetID属性设置为单击的窗口小部件的ID。

  9. On the OKButton_Click event or CancelButton_Click event on the server, hide the ModalPopup. If OKButton was clicked, call WidgetManipulator.SaveChanges() first.
  10. 在服务器上的OKButton_Click事件或CancelButton_Click事件中,隐藏ModalPopup。如果单击OKButton,请首先调用WidgetManipulator.SaveChanges()。

The part I haven't figured out is: How the heck do I know what widget was clicked on, and how do I pass that back to the server when I refresh the UpdatePanel? Is this even the right approach at all?

我没想到的部分是:我怎么知道点击了什么小部件,以及在刷新UpdatePanel时如何将其传递回服务器?这甚至是正确的方法吗?

2 个解决方案

#1


0  

When I need to pass data from client to server in ASP.NET AJAX, I generally use an asp:HiddenField with runat="server". Both can see it freely, but beware potential postback asynchronicity.

当我需要在ASP.NET AJAX中将数据从客户端传递到服务器时,我通常使用带有runat =“server”的asp:HiddenField。两者都可以*地看到它,但要注意潜在的回发异步性。

Sounds like you need to notify the server the widget was clicked - You may use a Timer to postback; or I'd go with option 5.

听起来你需要通知服务器点击了小部件 - 你可以使用Timer来回发;或者我选择5。

#2


1  

If you can use jQuery instead you could do something along the lines of these two posts:

如果您可以使用jQuery,那么您可以按照这两个帖子的方式做一些事情:

#1


0  

When I need to pass data from client to server in ASP.NET AJAX, I generally use an asp:HiddenField with runat="server". Both can see it freely, but beware potential postback asynchronicity.

当我需要在ASP.NET AJAX中将数据从客户端传递到服务器时,我通常使用带有runat =“server”的asp:HiddenField。两者都可以*地看到它,但要注意潜在的回发异步性。

Sounds like you need to notify the server the widget was clicked - You may use a Timer to postback; or I'd go with option 5.

听起来你需要通知服务器点击了小部件 - 你可以使用Timer来回发;或者我选择5。

#2


1  

If you can use jQuery instead you could do something along the lines of these two posts:

如果您可以使用jQuery,那么您可以按照这两个帖子的方式做一些事情: