1

I have a huge code behind file for one of my ASP.NET pages. It would be easier to maintain the code if I could break it up into multiple partial classes. However this is not well documented for ASP.NET.

I've learned that the additional partial classes must be moved into the App_Code folder. It seems that I need to use Protected WithEvents declarations to reference my web controls (although I'm not sure that will work yet).

My hang up right now is the ViewState. I cannot reference that in the additional partial class file. I need to get an ID number from the query string in the partial class. If I create a public property in the code behind file for the query string value, it cannot be referenced in the partial class file. It does not show up.

4

3 回答 3

2

Sounds like you need to create some classes to encapsulate some of your logic. You can always import the Web specific assemblies into your custom classes should you need to effect changes to the controls on your page.

于 2009-02-15T18:36:47.527 回答
0

If your code-behind file is really that big, then you probably need to push some of the logic out into other classes. Learn the Model-View-Controller pattern and investigate the ASP.NET MVC framework to see how to better lay out your objects.

It's a big code smell in ASP.NET to drop everything into code-behind files. It leads to non-scalable and unmaintainable applications.

于 2009-02-15T18:40:37.033 回答
0

分层思考是一个好方法。考虑您如何与数据库交互以及您正在为哪些对象检索数据,并为它们创建“数据层”类。他们的目的严格来说应该是与数据库交互,并返回可能包含适当数据的数据集(创建一个标准并遵循它!)。然后考虑您对该对象的数据执行哪些业务规则,并创建一个“业务层”类,其中包含调用您的数据层类并在该层中应用这些规则的方法。此时,数据应该已准备好显示,您的“表示层”(代码隐藏)负责执行的位置/时间/方式。

我的解释不是很好,但希望你能明白。

于 2009-02-15T19:13:06.940 回答