【问题标题】:Get RadListBox ID Value in C# to Change Textbox.Text在 C# 中获取 RadListBox ID 值以更改 Textbox.Text
【发布时间】:2014-09-10 01:17:03
【问题描述】:

我有一个 RadListBox,它有一个显示 display_text 的 itemTemplate。我正在使用 SQLDataSource 从表中获取所有数据。我想知道如何在单击列表框项目并触发 SelectedIndexChanged 后从 C# 端获取 ID 以更改 TextBox.Text。 我的 SQL 数据源有一个名为 ID 的列,我想获取该 ID 并将其设置在 _carID 中。类似 int _carID = LBcarDetail.FindControl("ID").Value; //(这是不正确的,只是可能使用的类似内容的示例,但我不确定确切的语法。)

<telerik:RadListBox ID="LBcarDetail" runat="server" DataSourceID="SqlDataSource1" AllowTransfer="true" AutoPostBackOnTransfer="true" OnSelectedIndexChanged="LBcarDetail_SelectedIndexChanged"
    Height="700px" SelectionMode="Multiple" Width="615px" Skin="Outlook" EnableDragAndDrop="true" AllowReorder="true" AppendDataBoundItems="true" AutoPostBack="true">
    <ButtonSettings VerticalAlign="Top" ShowTransferAll="false" ShowTransfer="false" ShowDelete="true"></ButtonSettings>
    <ItemTemplate>
        <div style="border: 1px solid black; margin-right: auto; margin-left: auto;">
            <ul class="details1" style="list-style: none;">
                <li>
                    <label>
                        display_text:
                    </label>
                    <span>
                        <%# Eval("display_text") %></span>
                </li>
            </ul>
        </div>
    </ItemTemplate>
</telerik:RadListBox>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:iConnectionString %>"
    SelectCommand="SELECT * FROM [intranet].[dbo].[TableItems] ORDER BY sort_order"></asp:SqlDataSource>

我正在尝试使用以下内容更改显示文本:

    protected void LBcarDetail_SelectedIndexChanged(object sender, EventArgs e)
        {
    //int _carID = 2; //hardcoded 2 will change display text to the correct value for item with ID of 2
    int _carID; //here is where I am trying to grab the ID of the listBox item that was selected
    string _displayText;

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["iConnectionString"].ToString()))
        using (SqlCommand comm = new SqlCommand("sp_car_detail_get", conn))
        using (SqlDataAdapter adapter = new SqlDataAdapter(comm))
        {
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.Add(new SqlParameter("@id", id));

            DataSet ds = new DataSet();
            adapter.Fill(ds);
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                _displayText = r["display_text"].ToString();

                tbDisplayText.Text = _displayText;
            }
        }
}

【问题讨论】:

    标签: c# asp.net listbox telerik selectedindexchanged


    【解决方案1】:

    我将 DataValueField="id" 添加到 RadListBox 并使用它来获取值:

    int id = Convert.ToInt32(LBcarouselDetail.SelectedItem.Value);
    

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2014-01-25
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      相关资源
      最近更新 更多