We take pride in your success. We let our positivity drive us, day in and out. Talk to us at Mindfire to know us more.

Software Technology Tips

    By default ListView does not maintain state of check boxes inside it. Suppose you are having checkboxes inside each item in ListView and a DataPager control to show all the records in page wise manner.Now, if you check some boxes on a page, go to another and come back to the same page, you will find that the checkboxes are not checked!  
How to Maintain the state of the Checkboxes

You need to store the value of the IDs to viewstate and again check the checkboxes according to the IDs in viewstate

Here is the ListView Control

<asp:ListView ID="lvExample" runat="server"
                        OnItemDataBound="lvExample_ItemDataBound"
 OnPagePropertiesChanging="lvExample_PagePropertiesChanging"
                        DataKeyNames="PK_ID">


Store the IDs in the view state
  private List<int> IDs
        {
            get
            {
                if (this.ViewState["IDs"] == null)
               {
                    this.ViewState["IDs"] = new List<int>();
                }
                return (List<int>)this.ViewState["IDs"];
            }
        }

When page porperty changes then add the IDs to viewState


  protected void AddRowstoIDList()
       {
         foreach (ListViewDataItem lvi in lvExample.Items)
          {
           CheckBox chkSelect = (CheckBox)lvi.FindControl("chkSelect");
           if ((((chkSelect) != null)))
             {
  int ID = Convert.ToInt32(lvExample.DataKeys[lvi.DisplayIndex].Value);

           //Check if the ID is already there
           if ((chkSelect.Checked && !this.IDs.Contains(ID)))
            {
              this.IDs.Add(ID);
            }
          else if ((!chkSelect.Checked && this.IDs.Contains(ID)))
           {
              this.IDs.Remove(ID);
            }
           }
          }
        }

When page property changes(going to another page) then add the IDs of previously checked checkboxes


protected void lvExample_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
 AddRowstoIDList();
}

In ListView Item Databound display the previously checked checkboxes according to the IDs stored in the Viewstate while databinding


protected void lvExample_ItemDataBound(object sender, ListViewItemEventArgs e)
{
      ListViewDataItem lvi = (ListViewDataItem)e.Item;
      if (lvi.ItemType == ListViewItemType.DataItem)
       {
         // Get each checkbox Listview Item on DataBound
        CheckBox chkSelect = (CheckBox)lvi.FindControl("chkSelect");

         // Make sure we're referencing the correct control
        if ((((chkSelect) != null)))
          {
          // If the ID exists in our list then check the checkbox
    int ID = Convert.ToInt32(lvExample.DataKeys[lvi.DisplayIndex].Value);
                    chkSelect.Checked = this.IDs.Contains(ID);
                }
            }
 }

 


Related Tags:

ASP.NET

Author: Bibhuti Chanda

top

ASP.NET

Let us Connect!

privacy

copyright (c) Mindfire Solutions 2007-2012. Login