【问题标题】:How to bind TextBox text to RadListBox如何将 TextBox 文本绑定到 RadListBox
【发布时间】:2014-05-02 05:12:10
【问题描述】:

我有两个RadListBoxes,即radListBoxSource和RadListBoxDestination。

这里我将radListBoxSource项目与DataSource绑定并转移到RadListBoxDestination。

现在我想添加一些从TextBox 到RadListBoxDestination 的文本,这些数据不在radListBoxSource 中。

为此,我添加了TextBox 和Button。
请告诉我如何将TextBox 数据和radListBoxSource 数据绑定到RadListBoxDestination。

.aspx:

<telerik:RadListBox runat="server" ID="radListBoxSource" Height="350px" Width="250px"
                    EnableDragAndDrop="true" TransferMode="Move" SelectionMode="Multiple" AllowTransfer="true"
                    TransferToID="RadListBoxDestination" AutoPostBackOnTransfer="true" DataSortField="CandidateColumn"
                    DataKeyField="ColumnID" AllowReorder="true">
    <EmptyMessageTemplate>
        No columns exist
    </EmptyMessageTemplate>
    <HeaderTemplate>
        <table width="100%">
            <tr>
                <td style="font-weight: bold; text-align: left;">
                    Candidate Columns
                </td>
            </tr>
        </table>
    </HeaderTemplate>
    <ItemTemplate>
        <div style="width: 100%;">
            <%# Eval("CandidateColumn") %>
        </div>
    </ItemTemplate>
</telerik:RadListBox>
<telerik:RadListBox ID="RadListBoxDestination" Height="350px" Width="250px" runat="server"
                    SelectionMode="Multiple" EnableDragAndDrop="true" AllowReorder="true">
    <EmptyMessageTemplate>
        No columns exist
    </EmptyMessageTemplate>
    <HeaderTemplate>
        <table width="100%">
            <tr>
                <td style="font-weight: bold; text-align: left;">
                    Candidate Columns
                </td>
            </tr>
        </table>
    </HeaderTemplate>
    <ItemTemplate>
        <div style="width: 100%;">
            <%# Eval("CandidateColumn") %>
        </div>
    </ItemTemplate>
</telerik:RadListBox>

<table>
    <tr>
        <td>
            <asp:Label ID="lblText" runat="server" Text="Enter Custom Field Name" Font-Bold="true"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtFieldName" runat="server" Width="175px"></asp:TextBox>
        </td>
        <td>
            <asp:Button ID="btnAdd" CssClass="form_btn_txt" runat="server" Text="Add" OnClick="btnAdd_Click" />
        </td>
    </tr>
</table>

.cs:

protected void btnAdd_Click(object sender, EventArgs e)
{
    RadListBoxItem test1 = new RadListBoxItem("item 1");
    test1.Text = txtFieldName.Text.Trim();
    RadListBoxDestination.Items.Insert(test1.Clone());              
}

我正在绑定 radListBoxSource 如下:

private void FillComboData()
{
    BizCandidate bizCandidates = new BizCandidate();
    DataSet dsCandidates = new DataSet();

    try
    {
        dsCandidates = bizCandidates.GetCandidateColumns();

        if (dsCandidates != null && dsCandidates.Tables.Count > 0)
        {
            if (dsCandidates.Tables[0].Rows.Count > 0)
            {
                radListBoxSource.DataSource = dsCandidates.Tables[0];
                radListBoxSource.DataValueField = "ColumnID";
                radListBoxSource.DataTextField = "CandidateColumn";
                radListBoxSource.DataBind();
            }
        }
    }
    catch { }
}

【问题讨论】:

  • 如果您从 RadListBoxDestination 中删除 ItemTemplate,它可能会起作用。但看起来你把事情搞混了。当您将某种数据源绑定到列表框时,将使用 Eval()。如何在后面的代码中设置两个列表框的默认值?此外,您不需要调用 .Clone()。您只是在创建一个新创建对象的克隆。
  • 这里我将项目从 radListBoxSource 移动到 RadListBoxDestination。
  • 好的,我已经写了答案。
  • 我在 btnAdd_Click 事件中编写的代码是否正确。请告诉我。
  • 是的,没错。但是您应该删除 .Clone() 调用。它什么也没做。只需复制新创建的对象即可。

标签: c# asp.net telerik radlistbox


【解决方案1】:

从 RadListBoxDestination 中删除 itemTemplate,因为您没有为此列表框使用数据源:

<telerik:RadListBox ID="RadListBoxDestination" Height="350px" Width="250px" runat="server"
                    SelectionMode="Multiple" EnableDragAndDrop="true" AllowReorder="true">
    <EmptyMessageTemplate>
        No columns exist
    </EmptyMessageTemplate>
    <HeaderTemplate>
        <table width="100%">
            <tr>
                <td style="font-weight: bold; text-align: left;">
                    Candidate Columns
                </td>
            </tr>
        </table>
    </HeaderTemplate>
</telerik:RadListBox>

【讨论】:

    猜你喜欢
    • 2011-05-20
    • 2011-01-28
    • 1970-01-01
    • 2018-02-09
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 2019-12-05
    相关资源
    最近更新 更多