Tuesday, July 12, 2005

Getting a DataView to work in an ASP.Net WebForm

With WinForms, I have consistently used dataview controls to allow me to quickly change the sort order or filter of a dataset as seen in a datagrid. It has always worked well for me.

I have not been nearly as successful with WebForms. Whenever I placed a dataview control on a WebForm, it would honor the initial settings of the dataview, but then ignore any runtime changes to those settings. I confirmed that I was properly filling the dataset after each form post.

So how is it that essentially the same exact steps work fine in Windows, but fails in IIS? To get around it I would dynamically compose a new SQL query and load the dataset to match the required sort and filter. This required more coding into the WebForm than really should have been necessary.

I searched extensively for a resolution to this situation but did not find one. Finally, I learned that the trick is to refrain from using the design-time property settings for the dataview when used in a WebForm. Leave the needed properties of the dataview blank and set them only at run-time within code. In this way, it will honor each change in setting and provide the same flexibility to quickly and easily change the view of the data as seen in the datagrid.

Please post a comment to request sample code that illustrates this if you want to see some. I'll put together a good example if there is interest.

Joe Kunk

No comments: