使用 Repeater 控件的可变列数 ASP.NET 3.5
其中一个页面包含一个 Repeater
控件。早些时候,我将静态数量的列绑定到中继器。例如,我的存储过程将返回员工的姓名、年龄、薪水、电话、出生日期。所以我可以像下面这样使用
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblSalary" runat="server" Text='<%#Eval("Salary")%>' ToolTip="Salary"></asp:Label>
</td>
</tr>
</ItemTemplate>
现在我想改变设计。我有一个设置页面,我会说明应在此处列出哪些列。有时我只需要列出姓名和年龄等。所以我不能对 Repeater
的设计进行硬编码。处理这种情况的最佳方法是什么?动态添加标签到item模板好不好?
One of pages contains a Repeater
control. Earlier, I bind a static number of columns to the repeater. For example, my stored procedure will return Name,Age,Salary,Phone,DOB of an employee. So I could use like the following
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblSalary" runat="server" Text='<%#Eval("Salary")%>' ToolTip="Salary"></asp:Label>
</td>
</tr>
</ItemTemplate>
Now I want to change the design. I have a settings page and I will say which columns should be listed here. Some time I need to list only Name and Age etc. So I can not hard code the design of Repeater
. What is the best way to handle the situation ? Is it good to dynamically add labels to the item template?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是为每个可能显示的字段设置一个用户控件。是否由同一用户控制所有可配置的字段,或者为每个字段提供单独的控制。然后,当您绑定转发器时,您可以相应地设置控件的
Visible
属性。这是你想去的方向吗?
One approach is to have a user control for each field that may be displayed. Whether the same user controls for all the field that is configurable or a separate control for each field. Then when you bind the repeater you can set the
Visible
property of the control accordingly.Is that the direction you wanted to go?
您可以将 GridView 与 TemplateFields 一起使用。根据您显示或隐藏列的设置。您可以在此处找到TemplateField 的文档。
编辑:另一个比 Repeater 更灵活的控件是 ListView。
如果您想使用中继器控件,您可以使用占位符来打开和关闭单个列。只需将每一列放入占位符中并打开和关闭可见性即可。当然,您也可以使用 UserControl 来代替 PlaceHolder。
编辑 2:使用 PlaceHolder 的解决方案可能如下所示:
You could use a GridView with TemplateFields. Depending on the settings you show or hide the columns. Here you find a documentation of the TemplateField.
EDIT: Another control with more flexibility than Repeater is the ListView.
If you want to use the Repeater Control you can use placeholders to turn on and off single columns. Just put every column into a PlaceHolder and turn on and off the visibility. Instead of a PlaceHolder you can of course use a UserControl as well.
EDIT 2: Solution with PlaceHolder could look like this: