Asp.net中滿足條件的複選框自動被選中(一)


一、有這樣一種需求:給單個用戶分組,分兩步走

1)加載所有用戶組

2)當前用戶已在的分組的複選框被勾選上

 

 

二、下面先演示一下想要的效果:

 

1、給單個用戶分組,選擇用戶:


2、彈框

查詢所有分類列表;把已分組的複選框選中,效果圖如下:


三、實現思路:

這裏唯一的難點就是,選擇的當前用戶已在的分組的複選框被勾選上。下面說一下實現思路:這裏通過數據綁定+集合來實現的。

1)圖一、圖二都是數據源綁定,選中圖一單個用戶,根據用戶code,去數據庫中查詢用戶的分組記錄,放入集合中供用戶圖二多選使用;

2)圖二根據綁定的數據,遍歷每一個複選框的數據,跟集合匹配;如果有和集合的數據相等的,對應的複選處於被選中的狀態。

 

四、代碼展示

客戶端代碼:

		 <table class="table table-striped">
		            <tr>
		                <th>
		                    <input type="checkbox" name="chkCode" value="" />編號
		                </th>
		                <th>
		                    用戶組名稱
		                </th>
		                <th>
		                    備註
		                </th>
		                <th>
		                    有效性
		                </th>
		            </tr>
		            <asp:Repeater ID="Repeater1" runat="server">
		                <ItemTemplate>
		                    <tr>                    
		                        <td>
		                            <input type="checkbox" name="chkCodes"  value="<%#Eval("Code") %>" <%#CheckBoxStatus(Eval("Code").ToString())%> /><%#Container.ItemIndex+1 %>
		                             
		                        </td>
		                        <td>
		                            <%#Eval("GroupName")%>
		                        </td>
		                        <td>
		                            <%#Eval("GroupMark")%>
		                        </td>
		                        <td>
		                            <%#Eval("ValidStatus") %>
		                        </td> 
		                    </tr>
		                </ItemTemplate>
		            </asp:Repeater>

        </table>


服務器端代碼:

		   #region  DataBinder()方法, Repeater1綁定用戶組
		        /// <summary>
		        /// Repeater1綁定用戶組
		        /// </summary>
		        protected  List<string> strRoleCodeList = new List<string>();
		        public void DataBinder()
		        {
		
		            //從用戶列表中傳過來要分組的用戶
		            string[] userCodes = Request.QueryString["userCode"].ToString().Split(',');
		            //查詢單個用戶的多分組集合code   
		            if (userCodes.Length == 1) {
		                Data.UserList_UserGroupCollection UL_UserGroup = Data.UserList_UserGroupAdapter.Instance.LoadUserList_UserGroupCollByUserCode(userCodes[0]);
		                foreach (var p in UL_UserGroup)
		                {
		                    strRoleCodeList.Add(p.UserGroupCode);
		                }
		            }
		            
		            //綁定所有用戶組列表
		           
		 int startRowIndex = (curPageIndex - 1) * AspNetPager1.PageSize;
		            int RecordCount = 0;
		            strSqlWhere = "1=1";
		            Data.UserGroupDataSource DefData = new Data.UserGroupDataSource();
		            Data.UserGroupCollection DefColl = DefData.Query(startRowIndex, AspNetPager1.PageSize, strSqlWhere, "CreateTime desc", ref RecordCount);
		            AspNetPager1.RecordCount = RecordCount;
		            Repeater1.DataSource = DefColl;
		            Repeater1.DataBind();
		               
		            }
		        #endregion
		        #region  CheckBoxStatus(string code) 方法; 前臺綁定此方法,檢查哪些複選框處於被選中的狀態
		        protected string CheckBoxStatus(string code)
		        {
		            if (strRoleCodeList.Contains(code))
		            { return "checked='checked'"; }
		            else
		            { return ""; }
		
		        }
		        #endregion


注意事項:一定先把該用戶的用戶集合拿到放入集合當中,然後進行綁定;原因:數據綁定的時候就會自動檢測集合當中是否存在與之該匹配的值,此時下面的查詢集合根本沒有執行,複選框自然匹配不上。

六、總結

站在巨人的肩膀上繼續前進纔是最明智的,呵呵。下篇將繼續完善 備選複選框--被選中的複選框靠前顯示。

 

發佈了119 篇原創文章 · 獲贊 4 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章