Andy Smith's Blog

Page.RegisterStartupScript('Andy', 'MetaBuilders_WebControls_GainKnowledge();');

Composite controls that do their own databinding

Most databound controls are written from the perspective that they will be databound to something, but the control writer doesn’t know what that would be. It is assumed that the control’s container will set the datasource and databind it. Therefore the same container knows about and handles the datasource, databinding, viewstate usages, event handling, etc. This works great.

But then application developers come along and want self contained controls, like, say… A composite control which contains a DropDownList that always contains items databound from a database, and a button that performs magic on those items. The app developer of course calls on his local control guru to make him this control.

This gives us 3 different people with 3 very different perspectives. There is the app developer, the custom control developer, and the asp.net team control developer who made the DropDownList control. The asp.net team guy does the Right Thing in exposing a DataSource property which DataBind uses, and an EnableViewState property which says whether or not the items created should be serialized to ViewState. He’s thinking, “If a consumer of my control does want to use viewstate, they can just bind on every request.” This is a reasonable position. Then on the other end we have the app developer who is using the custom control. He had the custom control made exactly BECAUSE he doesn’t want to think about datasources and databinding and such. However, the trickiness presents itself because he does still want to be in control of ViewState usage. Maybe some specific page is hit so often that ViewState should be disabled to save on bandwidth. “My custom control should just get the data every hit, I can cache at the data-access level to make up for it.” This is also a reasonable position.

So now the custom control developer is in a bit of a pickle. The break in the chain is that the lower levels assume that one person would be both databinding and deciding on ViewState policy. That isn’t the case here.

Ignoring the ViewState issue for a second, the straightforward solution to this is that the composite control does a “if not postback then databind” thing during Load, and override its DataBind method to databind its child DropDownList to the database. But then what happens if the app developer sets EnableViewState to false? Obviously the control is broken. So the control dev needs to databind during load in both first-request and disabled-viewstate states. This mostly solves the problem; however there is one last problem that I just don’t see a good solution to. Think of the case where an app dev sets the EnableViewState property to false during PreRender. In this case the Load-time DataBind never runs on subsequent requests even though there won’t be ViewState to load and thus the control is broken. Now don’t think that I imagine this is a common scenario, but it’s unfortunate. The only thing I can think is that you’d have to push the responsibility of DataBind() onto the app dev in this case. It’s not something I particularly like, because I think the app dev shouldn’t have to worry about the datasources of controls contained by it’s composite control children. However, it’s the best I’ve come up with so far.

Any other ideas?

Comments

Caleb Jenkins said:

What about....
...on_load
if(this.EnableViewState == false)
{
DataBind();
}
else if(!Page.isPostBack && this.EnableViewState == true)
{
DataBind();
};

Would that work? Maybe I'm missing something.
# August 30, 2004 3:08 PM

Andy Smith said:

Yes, that's the solution suggested with "So the control dev needs to databind during load in both first-request and disabled-viewstate states. This mostly solves the problem;" the code can be simplified to:
if ( !Page.IsPostBack || !EnabledViewState ) {
DataBind();
}
# August 30, 2004 3:49 PM

deebee said:

Hi Andy

First of all my english is pretty bad so hopefully you understand my sentences here.
Great Controls saw them the first time today. I got an idea spinning around my head.
I tried (just experimental) to minimize db trips and cam up with the following:
on first postback get an arraylist from the db and save it in session. for subsequent changes on the list i updatet the db AND the arraylist in the session. if the list changes i rebound the control.

All it saves are db-reads so not a big win because of the maintenance...

So next thought was:

Why not use viewstate instead of session but i didn't found a possibitlity to add/remove an item dynamically to a bound control WITHOUT rebind it (Without rebinding would be a winner because (multiple) selections i made wouldn't be lost.

Any thought on that?



# September 2, 2004 8:38 AM

Al Carlay said:

Interesting topic -- here's my view on it.

You have to push the responsibility of DataBind or whatever properties need to be changed dynamically to the next calling parent, which in this case is the app dev. The app developer now assumes the role of a custom control guy -- I see no other way around it, otherwise you break the consumer/provider model inherent in every object oriented model -- a model that defines responsibility between layers or "perspectives" as you have called it. But this model is also built on the concept that the caller only knows about the callee. In other words, the viewpoint or perspective between objects is only one degree of seperation. Most programmers program between two or three and find out later that they can't re-use their objects.

Since there is a predictable "event model" in asp.net, the event chain trickles down in a hierarchal singly rooted tree, as you have mentioned -- the app dev gets called first, then the custom control guy, then the asp.net control. Unfortunately, if you break this calling chain, and start jumping between layers without first talking to your neighbor (app dev talks to control guy, control guy talks to asp.net control), then what you are doing is programming against the asp.net event model.
# September 18, 2004 2:14 AM

TrackBack said:

# October 6, 2004 8:06 PM

Vinyl Shutters said:

I agree, there doesn't seem to be a complete solution to this issue.  And as I am finding out in the process of building my own composite control with an optional dropdownlist on it, the selected value doesn't seem to survive the postback anyway.

# January 17, 2008 12:42 PM

Naveen Kumar said:

Does any one try to provide Inbuilt paging functionaality  in Datalist, Repeater web controls in asp.net. Even Microsoft Launches VS.net 2.0,3.0,3.5 version But they again dont provide the Paging functionalty in these control. Can any one tell me why the don't provide this?.

# February 16, 2008 12:41 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)