我有一个看起来像附图的ListBox。你可以看到Hasbro是ListBox中的第一个项目,然后是Matt,然后是Bif Ban,然后是Mike的Awesome Stuff。这些项是从SQL表生成的。在那个SQL表中我有一个listBox sort_order列,它只是每个项目被抛入listBox时出现的顺序。我编写了一个过程来更新包含项目的表,并将listBox sort_order设置为@sort_order。当我在listBox中移动项目并单击Update Sort Order按钮时,我想循环遍历listBox并将listBox sort_order设置为该项目在listBox中的当前位置。例如,Hasbro当前在listBox sort_order 1中,如果我将它移到底部(在Mike的真棒东西之后),那么它将在listBox sort_order 4中。我怎么能循环遍历每个项目并为其分配这个新的listBox sort_order数值?
我认为它会是这样的但是我无法获得int id和int sort_order值:
foreach (RadListBoxItem item in LBcarDetail.Items)
{
int id; //how do I access this value? //this is the primary key from my SqlDataSource table
int sort_order; //and this value? this would be the position in the listBox
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["iConnectionString"].ConnectionString))
using (SqlCommand comm = new SqlCommand("sp_car_detail_updateSort", conn))
{
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.Add(new SqlParameter("@id", id));
comm.Parameters.Add(new SqlParameter("@sort_order", sort_order));
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
}
这是我的RadListBox和SqlDataSource的样子:
<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>
答案 0 :(得分:0)
您可以使用的sort_order
sort_order = LBcarDetail.Items.IndexOf(item);