tags:

views:

128

answers:

3

I'm building a class that automatically loads a DB record, looks at each DB column name, and fills the associated textbox/label/literal/etc, if it has the same name. I got this idea from Rails, in case that helps clarify what I'm trying to do.

public string presentData(Page thisForm)
{
// .. for each column name
// ... thisForm.FindControl() happens
}

I can't seem to find the Control I want this way, and there could quite possibly be a better way to do this.

Edit: If I can save on performance by doing it another way, definitely suggest it. I have large forms, and a recursive FindControl would cause a growth in performance time.

+3  A: 

FindControl is not recursive, so it only finds top level controls, not controls that are nested within other controls. See this article for an example of a recursive version of FindControl.

Erik Hesselink
I thought about doing something like this, but I figured I'd ask to see if there was a better way. Nice link!
Jon Smock
I tried this and it seems to be working pretty well performance-wise. No major hit that I can see yet.
Jon Smock
A: 

Have you thought about filling your controls client side? JQuery might be a good place to start. You'd generate the Javascript/JQuery to do the populating on the server, then JQuery takes care of searching for the controls. Very efficient.

Dan Powley
I like the idea here. It makes sense to unload some of the burden to the client's machine. But ultimately we'd like the pages to load as fast as possible, so I'll have to look at the trade off (server time saved vs. client time saved per request)
Jon Smock
+1  A: 

My suggestion is to use a BindingSource bound to a DataSet instead of recursively finding each control and setting its value.

http://msdn.microsoft.com/en-us/baya8sx4.aspx

http://msdn.microsoft.com/en-us/library/801dxw2t.aspx

http://www.codeproject.com/KB/grid/BindSourceBindingNavCS.aspx

http://msdn.microsoft.com/en-us/library/fbk67b6z(VS.80).aspx?ppud=4

John Chuckran