C# 如何将数据从 aspx 页面传递到 ascx 模式弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12671/
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 can I pass data from an aspx page to an ascx modal popup?
提问by Erick B
I'm fairly new to ASP.NET and trying to learn how things are done. I come from a C# background so the code-behind portion is easy, but thinking like a web developer is unfamiliar.
我是 ASP.NET 的新手,并试图了解事情是如何完成的。我来自 C# 背景,所以代码隐藏部分很容易,但像 Web 开发人员一样思考是不熟悉的。
I have an aspx page that contains a grid of checkboxes. I have a button that is coded via a Button_Click event to collect a list of which rows are checked and create a session variable out of that list. The same button is referenced (via TargetControlID) by my ascx page's ModalPopupExtender which controls the panel on the ascx page.
我有一个包含复选框网格的 aspx 页面。我有一个通过 Button_Click 事件编码的按钮来收集检查哪些行的列表并从该列表中创建一个会话变量。我的 ascx 页面的 ModalPopupExtender 引用了相同的按钮(通过 TargetControlID),它控制 ascx 页面上的面板。
When the button is clicked, the modal popup opens but the Button_Click event is never fired, so the modal doesn't get its session data.
单击按钮时,模态弹出窗口会打开,但 Button_Click 事件永远不会被触发,因此模态不会获取其会话数据。
Since the two pages are separate, I can't call the ModalPopupExtender from the aspx.cs code, I can't reach the list of checkboxes from the ascx.cs code, and I don't see a way to populate my session variable and then programmatically activate some other hidden button or control which will then open my modal popup.
由于这两个页面是分开的,我无法从 aspx.cs 代码调用 ModalPopupExtender,我无法从 ascx.cs 代码访问复选框列表,并且我没有看到填充会话变量的方法然后以编程方式激活一些其他隐藏的按钮或控件,然后打开我的模态弹出窗口。
Any thoughts?
有什么想法吗?
采纳答案by DancesWithBamboo
All a usercontrol(.ascx) file is is a set of controls that you have grouped together to provide some reusable functionality. The controls defined in it are still added to the page's control collection (.aspx) durring the page lifecylce. The ModalPopupExtender uses javascript and dhtml to show and hide the controls in the usercontrol client-side. What you are seeing is that the click event is being handled client-side by the ModalPoupExtender and it is canceling the post-back to the server. This is the default behavior by design. You certainly can access the page's control collection from the code-behind of your usercontrol though because it is all part of the same control tree. Just use the FindControl(xxx) method of any control to search for the child of it you need.
所有的 usercontrol(.ascx) 文件都是一组控件,您将它们组合在一起以提供一些可重用的功能。在页面生命周期中,其中定义的控件仍会添加到页面的控件集合 (.aspx) 中。ModalPopupExtender 使用 javascript 和 dhtml 来显示和隐藏用户控件客户端中的控件。您所看到的是,ModalPoupExtender 正在客户端处理单击事件,并且它正在取消回发到服务器。这是设计的默认行为。您当然可以从用户控件的代码隐藏中访问页面的控件集合,因为它都是同一控件树的一部分。只需使用任何控件的 FindControl(xxx) 方法来搜索您需要的子控件。
回答by Erick B
Sorry, but I'm confused. You can't call an ascx directly, so...
对不起,但我很困惑。你不能直接调用 ascx,所以......
Is your modal code that you are calling from within the same page, like a hidden panel, etc;
是您在同一页面内调用的模式代码,例如隐藏面板等;
Or is it another aspx page that you are calling on a click event?
或者它是您在单击事件上调用的另一个 aspx 页面?
回答by Erick B
After some research following DancesWithBamboo's answer, I figured out how to make it work.
An example reference to my ascx page within my aspx page:
在对 DancesWithBamboo 的回答进行了一些研究之后,我想出了如何让它发挥作用。
我的aspx页面中对我的ascx页面的示例引用:
<uc1:ChildPage ID="MyModalPage" runat="server" />
The aspx code-behind to grab and open the ModalPopupExtender (named modalPopup) would look like this:
用于抓取和打开 ModalPopupExtender(名为 modalPopup)的 aspx 代码隐藏如下:
AjaxControlToolkit.ModalPopupExtender mpe =
(AjaxControlToolkit.ModalPopupExtender)
MyModalPage.FindControl("modalPopup");
mpe.Show();