使用 RadComboBox 的条件项模板

发布于 2024-08-16 12:23:06 字数 1469 浏览 3 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(1

秋凉 2024-08-23 12:23:06

您可以将“All Departments”RadComboBoxItem 定义为 集合中的静态项目。由于您已启用 AppendDataBoundItems 属性,因此您不想在控件绑定静态项之前绑定到数据源;否则,您将看到展开组合框时看到的空白区域。另外,使用 DataBinder.Eval(Container, "Text") 呈现 DepartmentAbbr 字段。由于您已将此字段设置为控件的 DataTextField,因此该值将始终呈现。如果没有,当控件绑定到静态项时,您将再次获得空白空间,因为它不知道 DepartmentAbbr 是什么;它只有一个文本字段。这是一个帮助您入门的示例:

<telerik:RadComboBox ID="RadComboBox1" runat="server"
    AppendDataBoundItems="True" 
    DataTextField="Abbr"
    AutoPostBack="True"
    DataValueField="DeptID" 
    HighlightTemplatedItems="true"
    NoWrap="true" 
    Width="250px">
    <Items>                
        <telerik:RadComboBoxItem runat="server" Text="All Departments" />
    </Items>
    <ItemTemplate>
        <div>
            <b><%# DataBinder.Eval(Container, "Text")%></b><%# Eval("Name", " - ({0})") %>
        </div>
    </ItemTemplate>
</telerik:RadComboBox>

public partial class _Default : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        RadComboBox1.Load += new EventHandler(RadComboBox1_Load);
    }

    protected void RadComboBox1_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Ensure the static items are already bound before assigning
            // new data to the DataSource property
            RadComboBox1.DataBind();

            var departments = new[] { 
                new { DeptID = 1, Abbr = "ACME", Name = "ACME Corporation" },
                new { DeptID = 2, Abbr = "MSFT", Name = "Microsoft Corporation" },
                new { DeptID = 3, Abbr = "GOOG", Name = "Google, Inc" }
            };
            RadComboBox1.DataSource = departments;
            RadComboBox1.DataBind();
        }
    }
}

希望有帮助!

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, use DataBinder.Eval(Container, "Text") to render the DepartmentAbbr field. Since you have set this field as the DataTextField 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:

<telerik:RadComboBox ID="RadComboBox1" runat="server"
    AppendDataBoundItems="True" 
    DataTextField="Abbr"
    AutoPostBack="True"
    DataValueField="DeptID" 
    HighlightTemplatedItems="true"
    NoWrap="true" 
    Width="250px">
    <Items>                
        <telerik:RadComboBoxItem runat="server" Text="All Departments" />
    </Items>
    <ItemTemplate>
        <div>
            <b><%# DataBinder.Eval(Container, "Text")%></b><%# Eval("Name", " - ({0})") %>
        </div>
    </ItemTemplate>
</telerik:RadComboBox>

public partial class _Default : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        RadComboBox1.Load += new EventHandler(RadComboBox1_Load);
    }

    protected void RadComboBox1_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Ensure the static items are already bound before assigning
            // new data to the DataSource property
            RadComboBox1.DataBind();

            var departments = new[] { 
                new { DeptID = 1, Abbr = "ACME", Name = "ACME Corporation" },
                new { DeptID = 2, Abbr = "MSFT", Name = "Microsoft Corporation" },
                new { DeptID = 3, Abbr = "GOOG", Name = "Google, Inc" }
            };
            RadComboBox1.DataSource = departments;
            RadComboBox1.DataBind();
        }
    }
}

Hope that helps!

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