简体   繁体   English

与标记后代码相比,在标记中声明数据源的区别?

[英]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.

相关问题 需要帮助声明代码隐藏控件 - Help needed declaring code-behind controls 将DataSource ID传递给复选框后面的代码 - Passing DataSource ID to code-behind on checkbox check ASP.NET-当标记使用本地化的代码时,为什么在代码隐藏中使用默认的resx文件? - ASP.NET - why is the default resx file being used in the code-behind when the localized one is being used by the markup? MVC中的代码隐藏 - Code-behind in MVC 尝试从MainWindow后台代码绑定到UserControl后台代码时,数据绑定失败 - Data binding failing when trying to bind from MainWindow code-behind to UserControl code-behind WebForms按钮可以通过aspx标记访问类代码隐藏属性吗? - Can a WebForms button access a class code-behind property via the aspx markup? ASP.NET标记如何在代码隐藏中访问变量? - ASP.NET how does markup access to variables in code-behind? 单击按钮时用于运行代码隐藏代码的Javascript - Javascript for running code-behind code when a button is clicked 如何在代码后面使用GridView数据源不修剪文本? - How can I use the GridView datasource in code-behind to not trim text? 当在后面的代码中更改文本框时,文本框中的文本不会立即更新 - Text in a TextBox does not update instantly when change it in the code-behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM