I'm sure I'm missing something obvious here.

I have a about twenty data-bound GridViews, each of which share some common features. Let's say they're all lists of people, and every one starts with about 10 columns which are complex linkButtons with hovers etc, they're all the same. The remainder of these grids, which are extremely wide, consist of completely different columns; it's just the first 10 (of about 100) columns which are duplicated.

Hence I have this chunk of code which handles those 10 database columns and renders that html out, using 10 Eval() statements etc. It's the same code in each GridView, it's repeated 20 times over and that's obviously not cool.

I can't just chuck it in a user control or a custom control because I need the GridView to do the column headers and all that stuff. I can of course put a user control in for one column and render everything I want that way ("eval" works nested like that), but it doesn't give me the column headers, which are also complex.

So that's the question: is there any way I can break out of a templated GridView control to abstract this repeated code? I almost want a sort of server-side include or pre-processed thing, as I just need to repeat the text really, but I need to do it before asp.net tries to compile it. I could use a custom control to render any HTML I like, but that doesn't solve the problem as it's asp.net code I need to generate, not HTML.

有帮助吗?

解决方案

Can't you subclass from Gridview and implement the common functionality for all the 10 gridviews there and then have further subclasses that implement the specific functionality for the specific gridviews? I've done this successfully with Pages (the most base class had authentication logic that all subclasses inherited)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top