【问题标题】:Loop Through ListBox and Determine ListBox Item Order/Position循环遍历 ListBox 并确定 ListBox 项顺序/位置
【发布时间】:2023-03-14 14:48:02
【问题描述】:

我有一个看起来像所附图片的列表框。您可以看到 Hasbro 是 ListBox 中的第一项,然后是 Matt,然后是 Bif Ban,然后是 Mike's Awesome Stuff。这些项目是从 SQL 表生成的。在那个 SQL 表中,我有一个 listBox sort_order 列,它只是每个项目被扔进 listBox 时出现的顺序。我编写了一个更新包含项目的表并将 listBox sort_order 设置为@sort_order 的过程。当我在 listBox 中移动项目并单击“更新排序顺序”按钮时,我想遍历 listBox 并将 listBox sort_order 设置为该项目在 listBox 中的当前位置。例如,孩之宝目前在 listBox sort_order 1 中,如果我将它移到底部(在 Mike's Awesome Stuff 之后),那么它将在 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>

【问题讨论】:

    标签: c# asp.net foreach listbox telerik


    【解决方案1】:

    你可以使用的 sort_order

    sort_order = LBcarDetail.Items.IndexOf(item);
    

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      相关资源
      最近更新 更多