ASP.NET中擷取CheckBoxList的當前選擇項

來源:互聯網
上載者:User
CheckBoxList中有多個項,當選擇/不選擇某項時如果其AutoPostBack為True,則會觸發SelectedIndexChanged,但是CheckBoxList及其Items屬性都沒有直接能擷取當前選擇的項的屬性,想了一下,可以先將上一次的勾選狀態存到ViewState中,在觸發SelectedIndexChanged的時候進行比較,具體代碼如下:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title>無標題頁</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div>
  10.         
  11.         <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" 
  12.             onprerender="CheckBoxList1_PreRender" 
  13.             onselectedindexchanged="CheckBoxList1_SelectedIndexChanged" 
  14.             RepeatDirection="Horizontal">
  15.             <asp:ListItem>1</asp:ListItem>
  16.             <asp:ListItem>2</asp:ListItem>
  17.             <asp:ListItem Selected="True">3</asp:ListItem>
  18.             <asp:ListItem>4</asp:ListItem>
  19.             <asp:ListItem>5</asp:ListItem>
  20.         </asp:CheckBoxList>
  21.     </div>
  22.     </form>
  23. </body>
  24. </html>
  1. using System;
  2. using System.Collections.Generic;
  3. namespace WebApplication1
  4. {
  5.     public partial class _Default : System.Web.UI.Page
  6.     {
  7.         protected void Page_Load(object sender, EventArgs e)
  8.         {
  9.             Dictionary<int, bool> dic = new Dictionary<int, bool>();
  10.             for (int i = 0; i < CheckBoxList1.Items.Count; i++)
  11.                 dic.Add(i, CheckBoxList1.Items[i].Selected);
  12.             if (ViewState["cblChecked"] == null)
  13.                 ViewState["cblChecked"] = dic;
  14.         }
  15.         protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
  16.         {
  17.             if (ViewState["cblChecked"] != null)
  18.             {
  19.                 Dictionary<int, bool> dic = ViewState["cblChecked"] as Dictionary<int,bool>;
  20.                 for (int i = 0; i < CheckBoxList1.Items.Count; i++)
  21.                 {
  22.                     if (dic[i] != CheckBoxList1.Items[i].Selected)
  23.                         Response.Write("當前操作項為:" + i.ToString());
  24.                     dic[i] = CheckBoxList1.Items[i].Selected;
  25.                 }
  26.                 ViewState["cblChecked"] = dic;
  27.             }
  28.         }
  29.     }
  30. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.