与Repeater控件ASP.NET 3.5的列数的变量 [英] Variable number of columns with Repeater control ASP.NET 3.5
问题描述
一个页面包含一个转发
控制。早些时候,我列的静态绑定数到中继器。例如,我的存储过程将返回姓名,年龄,工资,电话,一个员工的出生日期。所以,我可以使用如下
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>
立即我要改变设计。我有一个设置页,我会说这列应该在这里列出。有些时候我需要仅列出姓名和年龄等,所以我不能硬code 转发
的设计。什么是处理这种情况的最好方法?它的好处是动态的标签添加到该项目模板?
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?
推荐答案
您可以用与使用TemplateFields的GridView。根据设置显示或隐藏的列。在这里,你找到的TemplateField 的文档。
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.
编辑:比直放站更灵活性的另一个控制是的ListView 。
Another control with more flexibility than Repeater is the ListView.
如果你想使用Repeater控件,您可以使用占位符来打开和关闭单个列。只要把每一个列到占位符,并打开和关闭的可见性。取而代之的是占位符,你当然可以用一个用户控件以及。
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.
编辑2:与占位符的解决办法是这样的:
EDIT 2: Solution with PlaceHolder could look like this:
<ItemTemplate>
<tr>
<asp:PlaceHolder Visible="<%# IsSalaryVisible %>" runat="server">
<td>
<asp:Label ID="lblSalary" runat="server" Text='<%#Eval("Salary")%>' ToolTip="Salary"></asp:Label>
</td>
</asp:PlaceHolder>
<asp:PlaceHolder Visible="<%# IsNameVisible %>" runat="server">
<td>
<asp:Label ID="lblSalary" runat="server" Text='<%#Eval("Name")%>' ToolTip="Salary"></asp:Label>
</td>
</asp:PlaceHolder>
</tr>
</ItemTemplate>
这篇关于与Repeater控件ASP.NET 3.5的列数的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!