简体   繁体   中英

Get RadListBox ID Value in C# to Change Textbox.Text

I have a RadListBox which has an itemTemplate that displays the display_text. I am using a SQLDataSource which grabs all of the data from a table. I would like to know how I can get the ID from the C# side to change the TextBox.Text after I click on an listBox item and SelectedIndexChanged is fired. My SQL datasource has a column named ID and I wanted to grab that ID and set it in the _carID. Something like int _carID = LBcarDetail.FindControl("ID").Value; //(this is not correct, just an example of something similar that may be used but I am not sure of the exact syntax.)

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

I am trying to change the display text using the following:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM