[英]Difference when declaring a DataSource in markup compared to code-behind?
I have a legacy solution where all the DataSource
s are defined in the markup for all .aspx-pages. 我有一个旧式解决方案,其中在所有.aspx页的标记中定义了所有
DataSource
。 The problem I have with this is that the DataSource
get instantiated several times since it is used in page, control... What I want is to have NInject to handle the instantiation of the DataSource
so this doesn't happen. 我的问题是,由于
DataSource
已在页面,控件中使用,因此实例化了几次。我想要的是让NInject处理DataSource
的实例化,这样就不会发生。 I have got that part to sort of work, but when the DataSource
is defined in the code-behind instead of in the markup the Page
property is not set on the DataSource
, does anyone know why? 我已经得到了部分排序的工作,但是当
DataSource
中的代码隐藏在标记,而不是被定义Page
属性未在设置DataSource
,没有人知道为什么吗?
A simple solution to this is to set the the Page
property in a base class or something, but I do think that the property should be there and want to know why. 一个简单的解决方案是在基类中设置
Page
属性,但是我确实认为该属性应该存在,并且想知道为什么。
You have to call DataBind()
after you set it. 设置后必须调用
DataBind()
。
WebControl1.DataSource = something;
WebControl1.DataBind();
It may has to do with the point in time of instantiation in the page lifecycle? 它可能与页面生命周期中的实例化时间有关吗? Can you monitor (debug) the instantiation in the code behind?
您可以监视(调试)后面代码中的实例化吗? Which callback are you using in the code behind?
您在后面的代码中使用哪个回调? If you are using the controls code behind, the Page property may not be set yet than.
如果您使用后面的控件代码,则可能尚未设置Page属性。 So choose a later callback - after the control has been added to some page already.
因此,请选择稍后的回调-在控件已添加到某个页面之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.