从以编程方式加载的子控件获取数据
我创建了 2 个控件来操作数据对象:1 个用于查看,另一个用于编辑。
在一页上,我加载“视图”UserControl
并以这种方式将数据传递给它:
ViewControl control = (ViewControl)LoadControl("ViewControl.ascx");
control.dataToView = dataObject;
this.container.Controls.Add(control);
这一切都很好,在控件内我可以抓取该数据并显示它。
现在我正在尝试遵循类似的编辑方法。我为此有一个不同的用户控件(带有一些用于编辑的文本框),我将原始数据传递给它,就像我对视图所做的那样:
EditControl control = (EditControl)LoadControl("EditControl.ascx");
control.dataToEdit = dataObject;
this.container.Controls.Add(control);
这也很好。
现在的问题是获取这些数据。当用户单击按钮时,我需要获取已编辑的数据并对其进行处理。 发生的情况是,由于控件是以编程方式添加的,因此用户更改的数据似乎无法在任何地方访问。
有解决办法吗?或者这种试图将事物分开并可能重复使用的方法是不可能的吗?
提前致谢。
I've created 2 controls to manipulate a data object: 1 for viewing and another for editing.
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);
Which also works fine.
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?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
动态创建的控件必须添加到页面上的
Init
或PreInit
事件中。您必须在此处执行此操作,因为尚未加载控件状态。如果您在之后添加控件,它们将会为空,因为页面在页面生命周期的早期就已经填充了控件页面值。
有关页面生命周期的详细信息此处
Controls created dynamically must be added on
Init
orPreInit
event on the page.You have to do it here because controls state is not yet loaded. If you add the the control AFTER you'll have them empty since the page already filled control page values early in the page life cycle.
More info about page life cycle here
这是我的解决方案:
在页面中,我保留对以编程方式加载的控件的引用。
在控件中,我创建了一个 GetData() 方法,该方法返回一个包含用户输入的数据的对象。
当用户提交表单时,页面调用控件上的 GetData() 方法来访问用户输入的数据。
Here's my solution:
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.
只需将对控件的引用保留为页面代码隐藏中的变量即可。即,
只要您在页面生命周期的正确部分(初始化)创建控件,
ViewState
就会得到维护。我假设 dataToEdit 只是一个TextBox
或者其他东西?Just keep a reference to the control as a variable in the page's code-behind. I.e.
As long as you're creating the control in the right part of the page's lifecycle (Initialization) then
ViewState
will be maintained. I'm assuming dataToEdit is just aTextBox
or something?