【问题标题】:How to move selected Checkbox to the top of a Checkbox List如何将选定的复选框移动到复选框列表的顶部
【发布时间】:2019-11-08 20:54:09
【问题描述】:

我有一个数据绑定复选框列表,并希望选中的复选框项移动到列表顶部。

我尝试过搜索,但所有解决方案都使用 html 复选框而不是 asp 复选框列表

这是我的复选框列表代码

<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="SBrand" DataValueField="SBrand" AutoPostBack="True" SelectedIndexChanged="gvStock_SelectedIndexChanged" OnSelectedIndexChanged="CheckBoxList_SelectedIndexChanged" OnPageIndexChanging="gvStock_PageIndexChanging" CssClass="checkboxlist">
</asp:CheckBoxList>

【问题讨论】:

    标签: c# asp.net checkbox checkboxlist


    【解决方案1】:

    您可以在 CheckBoxList 的 SelectedIndexChanged 事件中执行此操作。

    <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true" 
        OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"></asp:CheckBoxList>
    

    后面的代码

    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //get the index of the last changed checkbox
        int index = Convert.ToInt32(Request.Form["__EVENTTARGET"].Split('$').Last());
    
        //find the correct listitem in the checkboxlist
        ListItem item = CheckBoxList1.Items[index];
    
        //if the item is already in first position do nothing
        if (index == 0)
            return;
    
        //remove it from it's current position
        CheckBoxList1.Items.RemoveAt(index);
    
        //add the listitem at the top
        CheckBoxList1.Items.Insert(0, item);
    }
    

    这可能不适用于DataSourceID。因此,如果不是,您必须从后面的代码开始将数据绑定到 CheckBoxList:http://www.dotnetfox.com/articles/how-to-bind-data-to-checkboxlist-control-in-Asp-Net-using-C-Sharp-1042.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      相关资源
      最近更新 更多