开发者

ASP.NET WebForms - Control design (w.r.t. data)

I'm designing/building a medium-scale ASP.NET web application using WebForms. The main work in the project is building each web form, and I don't know how to design (in a code/data/data flow sense) my controls.

I'll give you a few examples of what I'm dealing with so you can understand what I mean:


A common task in the application is the entry and display of street addresses. So I created a UserControl called "AddressFields" that contains a series of HtmlInputText elements (one for line 1, another fo开发者_开发问答r the town/city, country, postal code, etc). My DB entities classes contains a class called "Address" so this control has the methods:

  • void ShowAddress(Address addr) - which fills the HtmlInputText elements with the appropriate text).
  • void UpdateAddress(Address addr) - which updates the contents of addr with the current contents of the HtmlInputText elements
  • Address CreateAddress() - which creates a new instance of Address and then passes it into UpdateAddress and then returns it

So far, so good. This works because the AddressFields control is 'dumb' all it does is display and retrieve data to the user. ViewState is managed by the HtmlInputText fields directly too, so no additional logic is required.


Another entity in my application is the "Client" which is a class with an Address attribute, but this class also has a few more complicated aspects. For example a Client has a series of tags ('categories' in my application) that can be assigned. In this case, I designed a subclass of the ASP.NET CheckboxList control.

I created another UserControl called "ClientFields" which contains all the necessary fields to display a Client's information (including an AddressFields control), but it also includes my CheckboxList subclass called "CategoryList".

The problem here is that the CategoryList control needs to be supplied to the data to display (but not on postbacks, since it uses viewstate). In this case, my question is: whose responsibility is it to connect to the database and retrieve the category listing?

Is it the CategoryList control? (If so, where in the control lifespan does it query the database? It can't do it in Control.Load because that occurs after the ClientFields has been populated. Does it occur in ClientFields itself? (again, then where in the lifespan does it happen, because ClientFields has its ShowClient(Client c) method called within Page.Load. An alternative is to expose the CategoryList from the ClientFields so it can be accessed directly by the Page, but that's a violation of good software design.


In my opinion, CategoryList is responsible to retrieve the category listing data because all of category data should be retrieved no matter if one or more of them are selected by a particular client.

when does CategoryList query the database in its lifespan?

Generally, override OnInit method to populate the CategoryList, but ViewState doesn't begin to work at that time, so you have to retrieve the data from DB and populate itself on each postback. If you'd like to rely on ViewState, define and register the InitComplete event handler of Page on overrided OnInit method and populate the CategoryList in the event handler. Both OnInit method and InitComplete event are called/fired before Load event.

//In CategoryList control
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    Page.InitComplete += new EventHandler(Page_InitComplete);
}

void Page_InitComplete(object sender, EventArgs e)
{
    // retrieve the data and populate the control
}

Hope it helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜