获取C#中的RadListBox ID值以更改Textbox.Text

时间:2014-09-10 01:17:04

标签: c# asp.net listbox telerik selectedindexchanged

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

<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;
            }
        }
}

1 个答案:

答案 0 :(得分:0)

我添加了DataValueField =&#34; id&#34;到RadListBox并使用它来获取值:

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