数据绑定控件中的重复代码

发布于 2024-12-09 02:48:16 字数 564 浏览 1 评论 0原文

我确信我在这里遗漏了一些明显的东西。

我有大约二十个数据绑定 GridView,每个都有一些共同的功能。假设它们都是人员列表,每个列表都以大约 10 列开始,这些列是带有悬停等的复杂链接按钮,它们都是相同的。这些网格的其余部分非常宽,由完全不同的列组成;它只是前 10 列(大约 100 列)是重复的。

因此,我有这段代码,它使用 10 个 Eval() 语句等来处理这 10 个数据库列并渲染该 html。每个 GridView 中的代码都是相同的,它重复了 20 次,这显然不酷。

我不能只是将其放入用户控件或自定义控件中,因为我需要 GridView 来执行列标题和所有其他内容。我当然可以将用户控件放入一列中并以这种方式呈现我想要的所有内容(“eval”像这样嵌套),但它没有给我列标题,这也是复杂的。

所以这就是问题:有什么方法可以突破模板化的 GridView 控件来抽象这些重复的代码吗?我几乎想要一种服务器端包含或预处理的东西,因为我只需要真正重复文本,但我需要在 ASP.NET 尝试编译它之前完成它。我可以使用自定义控件来呈现我喜欢的任何 HTML,但这并不能解决问题,因为我需要生成的是 asp.net 代码,而不是 HTML。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2024-12-16 02:48:16

难道您不能从 Gridview 派生子类并实现所有 10 个 gridview 的通用功能,然后再有更多的子类来实现特定 gridview 的特定功能吗?我已经使用 Pages 成功完成了此操作(最基本的类具有所有子类继承的身份验证逻辑)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文