使用 RadComboBox 的条件项模板
我有一个 RadComboBox,用于显示部门名称和缩写。我使用带有 LinqDataSource 的项目模板来使每个项目显示为:
DeptAbbr - (DeptName)
这是我用来执行此操作的代码,它工作正常:
<telerik:RadComboBox ID="rcbDepartments" runat="server" AppendDataBoundItems="True"
OnInit="rcbDepartments_Init" DataTextField="DepartmentAbbr" AutoPostBack="True"
DataSourceID="ldsDepartments" DataValueField="DepartmentID" HighlightTemplatedItems="true"
NoWrap="true" Width="250px">
<ItemTemplate>
<div>
<b>
<%# Eval("DepartmentAbbr")%></b><%# Eval("DepartmentName", " - ({0})") %>
</div>
</ItemTemplate>
</telerik:RadComboBox>
我的问题是这样的。我想在列表中添加一个初始项目,该项目适用于“所有部门”,并且是默认项目。我可以轻松做到这一点,但我遇到的问题是,因为我没有在数据库中存储“所有部门”条目,所以当您下拉组合框时,模板会在项目列表的开头显示一个空格。我想知道是否有任何方法可以模板化列表中除第一项之外的所有项目?
注意:我还尝试在 Eval 中执行如下条件:
<b><%# (Eval("DepartmentAbbr") != null) ? Eval("DepartmentAbbr") : "All Departments" %></b><%# Eval("DepartmentName", " - ({0})") %>
但它仅对数据绑定的项目进行评估,而不是对我手动粘贴的初始项目进行评估。换句话说,如果我将上述语句更改为:
<b><%# (Eval("DepartmentAbbr") == null) ? Eval("DepartmentAbbr") : "All Departments" %></b><%# Eval("DepartmentName", " - ({0})") %>
那么我只会得到一个列表,顶部有一个空白项目,其余项目为“所有部门”。
我解决这个问题的方法是使用 LINQ 服务器端做一些时髦的选择,但这迫使我放弃所有模板和 html 格式。
I have a RadComboBox that I am using to display department name and abbreviations. I am using an Item Template with a LinqDataSource to make each item appear as:
DeptAbbr - (DeptName)
Here is the code I am using to do this and it works fine:
<telerik:RadComboBox ID="rcbDepartments" runat="server" AppendDataBoundItems="True"
OnInit="rcbDepartments_Init" DataTextField="DepartmentAbbr" AutoPostBack="True"
DataSourceID="ldsDepartments" DataValueField="DepartmentID" HighlightTemplatedItems="true"
NoWrap="true" Width="250px">
<ItemTemplate>
<div>
<b>
<%# Eval("DepartmentAbbr")%></b><%# Eval("DepartmentName", " - ({0})") %>
</div>
</ItemTemplate>
</telerik:RadComboBox>
My question is this. I want to add an initial item in the list that is for "All Departments" and is the default item. I can do this easily, but the problem I'm having is that because I am not storing an "All Departments" entry in the database, the templating shows a blank space at the beginning of the items list when you pull down the combo box. I'm trying to find out if there is any way to template all but first item in the list?
Note: I have also tried do a conditional in the Eval like this:
<b><%# (Eval("DepartmentAbbr") != null) ? Eval("DepartmentAbbr") : "All Departments" %></b><%# Eval("DepartmentName", " - ({0})") %>
But it only evaluates on the items that are databound and not the initial item which I am sticking in manually. In other words, if I change the above statement to be:
<b><%# (Eval("DepartmentAbbr") == null) ? Eval("DepartmentAbbr") : "All Departments" %></b><%# Eval("DepartmentName", " - ({0})") %>
Then I just get a list with one blank item at the top and the rest reading "All Departments".
My work around for this problem has been to do some funky selection stuff with LINQ server side, but that has forced me to get rid of all templating and html formatting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将“All Departments”RadComboBoxItem 定义为
集合中的静态项目。由于您已启用 AppendDataBoundItems 属性,因此您不想在控件绑定静态项之前绑定到数据源;否则,您将看到展开组合框时看到的空白区域。另外,使用 DataBinder.Eval(Container, "Text") 呈现 DepartmentAbbr 字段。由于您已将此字段设置为控件的DataTextField
,因此该值将始终呈现。如果没有,当控件绑定到静态项时,您将再次获得空白空间,因为它不知道 DepartmentAbbr 是什么;它只有一个文本字段。这是一个帮助您入门的示例:希望有帮助!
You can define the 'All Departments' RadComboBoxItem as a static item in the
<Items>
collection. Since you have enabled the AppendDataBoundItems property, you don't want to bind to your data source until after the control has already bound the static items; otherwise you'll get the blank space you are seeing when expanding the combo box. Also, useDataBinder.Eval(Container, "Text")
to render the DepartmentAbbr field. Since you have set this field as theDataTextField
for the control, that value will always render. If not, you'll get the empty space again when the control binds to the static item because it doesn't know what DepartmentAbbr is; it only has a Text field. Here's an example to get you going:Hope that helps!