繁体   English   中英

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

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

我有一个RadListBox,其中有一个显示display_text的itemTemplate。 我正在使用一个SQLDataSource,它从表中获取所有数据。 我想知道如何在单击listBox项并触发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;
            }
        }
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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