![](/img/trans.png)
[英]getter setter can't change textBox.Text control value in Winform c#
[英]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.