保存Repeater控件中input CheckBox的选中状态

自主原创、个人使用请注明出处:请勿用于商业用途,保留法律追责权利!

展示前台: 


<fieldset>

        <legend>查询列表</legend>
        <div class="ListDiv">
            <table class="table" id="tabList">
                <thead>
                    <tr>
                        <th>
                            <input type="checkbox" id="chkHead" class="inputchk" />
                        </th>
                        <th style="text-align: left;">
                            姓名
                        </th>
                        <th style="text-align: left;">
                            学号
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <asp:Repeater ID="rp_list" runat="server">
                        <ItemTemplate>
                            <tr>
                                <td style="text-align: center;">
                                    <input type="checkbox" id="chkList" runat="server" />
                                </td>
                                <td>
                                    <input type="hidden" runat="server" id="hidRowId" value='<%# Eval("rowid")%>' />
                                    <%# Eval("student_name")%>
                                </td>
                                <td>
                                    <%# Eval("study_no")%>
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </tbody>
            </table>
            <CService:TPager ID="AspNetPager1" runat="server" OnPagerChanged="PageChanged">
            </CService:TPager>
        </div>

        </fieldset>


后台代码展示:



code


        /// <summary>
        /// 获取选中项的ID并保存在ViewState中
        /// </summary>
        public void GetSelectId()
        {
            Dictionary<string, string> dic = ViewState["rowid"] as Dictionary<string, string>;
            if (ViewState["rowid"] == null)
                dic = new Dictionary<string, string>();
            if (rp_list.Items.Count == 0)
                return;
            else
            {
                string id = string.Empty;
                if (dic == null)
                    dic = new Dictionary<string, string>();

                for (int i = 0; i < rp_list.Items.Count; i++)
                {
                    id = ((HtmlInputHidden)rp_list.Items[i].FindControl("hidRowId")).Value;
                    if (((HtmlInputCheckBox)rp_list.Items[i].FindControl("chkList")).Checked == true)
                    {
                        if (!dic.Keys.Contains(id))
                            dic.Add(id, id);
                    }
                    else
                    {
                        if (dic.Keys.Contains(id))
                            dic.Remove(id);
                    }
                }
                ViewState["rowid"] = dic;
            }
        }

        /// <summary>
        /// 恢复复选框选中状态
        /// </summary>
        public void ToRestoreTheSelectedItem()
        {
            string id = string.Empty;
            Dictionary<string, string> dic = ViewState["rowid"] as Dictionary<string, string>;
            for (int i = 0; i < rp_list.Items.Count; i++)
            {
                id = ((HtmlInputHidden)rp_list.Items[i].FindControl("hidRowId")).Value;
                HtmlInputCheckBox chk = ((HtmlInputCheckBox)rp_list.Items[i].FindControl("chkList"));

                if (dic.Keys.Contains(id))
                    chk.Checked = true;
            }
        }


在分页控件中的使用:

        /// <summary>
        /// 分页控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void PageChanged(object sender, EventArgs e)
        {
            GetSelectId();
            BindData(false);
            ToRestoreTheSelectedItem();
        }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值