UserControl编辑对象 [英] UserControl to edit an object
问题描述
你好,
我有一个UserControl,其中有一些用于编辑对象的字段,存储在后面的代码中的typed属性中,它的名称是DataSource.
假设,此控件允许用户查看对象并对其进行编辑; objact也有一个列表,并且控件应允许通过复选框列表对其进行编辑.
在控件onLoad中,我设置了复选框列表的数据源
将该控件放在aspx页面中,在onLoad事件中设置DataSource属性,然后调用this.DataBind();
问题是,在我的用户控件中,onDataBinding是在onLoad之前执行的,
在UserControl中出现问题,因为onLoad是在页面onLoad事件之后执行的,因此onDataBinding最初是onLoad.
最后,未设置childs控件的数据源.
这是常见的情况吗?有人有同样的问题吗?我做错了方法吗?
谢谢!
Hello,
i have a UserControl that has some fields used to edit an object, stored in a typed property in the code behind, it''s name is DataSource.
Let''s say, this control allow the user to have a view of the object and edit it; the objact has a list too, and the control should allow to edit it through a checkboxlist.
In the control onLoad i set the DataSource of the checkboxlist
This control is put inside an aspx page, i set the DataSource property in the onLoad event, and then i call this.DataBind();
The problem is that in the my usercontrol onDataBinding is executed before onLoad,
Il problema è che nel mio UserControl viene eseguito onDataBinding prima di onLoad, because onLoad is executed after the page onLoad event
In the end the datasources of the childs controls are not set.
Is this a common situation? Someone had the same problem? am I doing wrong approach?
thank you!
推荐答案
我找到了问题的根源和解决方案. 我有一个Repeater,并且在itemtemplate里面放了我的用户控件.
UuserControls不实现ITemplate,因此用作模板时它们具有异常行为.
我们必须在转发器数据绑定之前将.ascx控件加载为模板:
Repeater1.ItemTemplate = LoadTemplate("ucitemtemplate.ascx");
Repeater1.DataSource =表;
Repeater1.DataBind();
这里是有关此的很好的文档:
http://msdn.microsoft.com/zh-cn/magazine/cc163780.aspx#S3
这是关于如何构建用户控件的另一个很好的文档
http://msdn.microsoft.com/zh-cn/library/ms972975.aspx
I found the source of the problem and the solution..
I had a Repeater, and inside the itemtemplate i put the my usercontrol.
The UuserControls do not implements the ITemplate, so used as template they have an an abnormal behavior.
We have to load the .ascx control as tamplate before the repeater databind:
Repeater1.ItemTemplate = LoadTemplate("ucitemtemplate.ascx");
Repeater1.DataSource = table;
Repeater1.DataBind();
Here a good document about this:
http://msdn.microsoft.com/en-us/magazine/cc163780.aspx#S3
and here another good document abount how to build user controls
http://msdn.microsoft.com/en-us/library/ms972975.aspx
这篇关于UserControl编辑对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!