以编程方式从子控件中获取数据 [英] Getting data from child controls loaded programmatically

查看:61
本文介绍了以编程方式从子控件中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了2个控件来操作数据对象:1个用于查看,另一个用于编辑.

I've created 2 controls to manipulate a data object: 1 for viewing and another for editing.

在一页上,我加载视图" UserControl 并以这种方式将数据传递给它:

On one page I load the "view" UserControl and pass data to it this way:

ViewControl control = (ViewControl)LoadControl("ViewControl.ascx");
control.dataToView = dataObject;
this.container.Controls.Add(control);

这很好,在控件内部,我可以获取数据并显示它.

That's all fine and inside the control I can grab that data and display it.

现在,我正在尝试采用类似的方法进行编辑.为此,我有一个不同的用户控件(带有一些用于编辑的文本框),我将原始数据传递给该用户控件的方式与对视图的操作相同:

Now I'm trying to follow a similar approach for editing. I have a different User Control for this (with some textboxes for editing) to which I pass the original data the same way I did for the view:

EditControl control = (EditControl)LoadControl("EditControl.ascx");
control.dataToEdit = dataObject;
this.container.Controls.Add(control);

哪个也可以.

现在的问题是获取这些数据.当用户单击一个按钮时,我需要获取已编辑的数据并对其进行处理.发生的事情是,由于控件是通过编程方式添加的,因此用户更改的数据似乎无法在任何地方访问.

The problem now is getting to this data. When the user clicks a button I need to fetch the data that was edited and do stuff with it. What's happening is that because the controls are being added programmatically, the data that the user changed doesn't seem to be accessible anywhere.

有解决方案吗?还是这种试图使事物分离并可能重复使用的方法是不可能的?

Is there a solution for this? Or is this way of trying to keep things separated and possibly re-usable not possible?

提前谢谢.

推荐答案

这是我的解决方案:

在页面中,我保留了对以编程方式加载的控件的引用.在控件中,我创建了一个GetData()方法,该方法返回一个带有用户输入数据的对象.当用户提交表单时,页面将调用控件上的GetData()方法以访问用户输入的数据.

In the page, I keep a reference to the control that is loaded programmatically. In the control I created a GetData() method that returns an object with the data entered by the user. When the user submits the form, the page calls the GetData() method on the control to access the data the user entered.

这篇关于以编程方式从子控件中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆