asp.netcheckboxpagination

How to remember the selected checkboxes between each paging-press


I have a listview with paging. Every paging has 10 rows with with a username, an email and a checkbox.

I need to be able to check a couple of checkboxes, in different "pagings", press a button and send an email every to ever user that has been checked.

Trouble is I don't really know how to remember the selected checkboxes between each paging-press.

Are you with me?

Deos anyone have a similar solution or a few tips on how to do this? I'd prefer to solv this without jQuery, but ordinare javascript or a C# solution works fine.

Thanks in advance!


Solution

  • Try this http://forums.asp.net/t/1619741.aspx?Remember+checkboxes+in+ListView

    you could maintain a List<> of selections (say ID to each record, unique value) and would have to persist that list in the ViewState. update the list on a page change. bind the checkbox checked property through a code-behind function that checks whether the id is in the list.

    here's a sample:

            <asp:ListView ID="ListView1" runat="server" OnPagePropertiesChanging="ListView1_PagePropertiesChanging">
                <LayoutTemplate>
                    <table id="Table2" runat="server" class="listview" width="100%">
                        <tr>
                            <td>
                                <table id="itemPlaceholderContainer" runat="server">
                                    <tr>
                                        <th>
                                        </th>
                                        <th>
                                        </th>
                                        <th id="thid" runat="server" visible="false">
                                            ID
                                        </th>
                                        <th id="thname" runat="server">
                                            Name
                                        </th>
                                    </tr>
                                    <tr id="itemPlaceholder" runat="server">
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr id="Tr3" runat="server">
                            <td id="Td2" runat="server" style="">
                                <asp:DataPager ID="DataPager1" runat="server" PageSize="5" PagedControlID="ListView1"
                                    OnPreRender="DataPager1_PreRender">
                                    <Fields>
                                        <asp:NumericPagerField />
                                    </Fields>
                                </asp:DataPager>
                            </td>
                        </tr>
                    </table>
                </LayoutTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <input type="checkbox" id="CheckBox1" runat="server" value='<%# Eval("ID") %>' checked='<%# Selected(Eval("ID")) %>' />
                        </td>
                        <td>
                            <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>' />
                        </td>
                        <td>
                            <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' />
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:ListView>
    
    code-behind:
    
            List<string> cBoxSelections = new List<string>();
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindListView();
                }
            }
    
            private void BindListView()
            {
                ListView1.DataSource = CreateDataSource();
                ListView1.DataBind();
            }
    
            protected DataTable CreateDataSource()
            {
                DataTable dt = new DataTable();
                DataRow dr;
                dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
                dt.Columns.Add(new DataColumn("Name", typeof(string)));
                for (int i = 0; i < 33; i++)
                {
                    dr = dt.NewRow();
    
                    dr[0] = i;
                    dr[1] = string.Concat("Name",i.ToString());
                    dt.Rows.Add(dr);
                }
                return dt;
            }
    
            protected Boolean Selected(object sender)
            {
                Boolean flag = false;
                string ID = Convert.ToString(sender);
                if (!string.IsNullOrEmpty(ID))
                {
                    flag = cBoxSelections.Exists(item => item.Equals(ID));
                }
                return flag;
            }
    
            private void UpdateSelections()
            {
                if (ViewState["sel"] != null)
                {
                    cBoxSelections = (List<string>)ViewState["sel"];
                }
                foreach (ListViewDataItem item in ListView1.Items)
                {
                    HtmlInputCheckBox cbox = (HtmlInputCheckBox)item.FindControl("CheckBox1");
                    if (cbox != null)
                    {
                        string selectedItem = cBoxSelections.Find(key => key.Equals(cbox.Value));
                        if (selectedItem == null)
                        {
                            if (cbox.Checked.Equals(true))
                            {
                                cBoxSelections.Add(cbox.Value);
                            }
                        }
                        if (selectedItem != null)
                        {
                            if (cbox.Checked.Equals(false))
                            {
                                cBoxSelections.Remove(cbox.Value);
                            }
                        }
                    }
                }
                if (cBoxSelections.Count > 0)
                {
                    ViewState["sel"] = cBoxSelections;
                }
            }
            protected void DataPager1_PreRender(object sender, EventArgs e)
            {
                BindListView();
            }
    
    
        protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            UpdateSelections();
        }
    

    you need the routines Selected (that returns a boolean) and UpdateSelections (which maintains), the rest of the code simply gets the ListView to demonstrate the sample along with dummy data.