Maintain the state(userid) of checked records during paging in rad grid?

10 posts, 0 answers
  1. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 16 Aug 2013 Link to this post

    Hi,
    How to Maintain the state(userid) of checked records  during paging in rad grid?
    below is my code for reference.
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowSelection="True" AllowPaging="True"
               PageSize="5" AllowSorting="True"
               OnNeedDataSource="RadGrid1_NeedDataSource" CellSpacing="0"
               GridLines="None" ShowGroupPanel="True">
               <PagerStyle Mode="NextPrevAndNumeric" AlwaysVisible="true"></PagerStyle>
               <ClientSettings Selecting-AllowRowSelect="true" AllowDragToGroup="True">
                   <Selecting AllowRowSelect="True"></Selecting>
               </ClientSettings>
               <MasterTableView DataKeyNames="UserId">
                   <Columns>
                       <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" />
                   </Columns>
                   <PagerStyle AlwaysVisible="True"></PagerStyle>
               </MasterTableView>
           </telerik:RadGrid>
           <br />
           <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" Width="130px"
               Height="25px" ValidationGroup="val" />
           <br />
    and
    public partial class GroupUserCreation : System.Web.UI.Page
    {
        Globas obj = new Globas();
        CheckBox chkbox = new CheckBox();
        string GroupId="";
      
     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
     
                DataTable dt1 = obj.GroupDetails();
                cmbgroupname.DataSource = dt1;
                cmbgroupname.DataTextField = "GroupName";
                cmbgroupname.DataValueField = "GroupId";
                cmbgroupname.DataBind();
            }
          
            
        }
     
        protected void Button1_Click(object sender, EventArgs e)
        {
            
            try
            {
                GroupId = cmbgroupname.SelectedItem.Value.ToString();
                foreach (GridItem item in RadGrid1.MasterTableView.Items)
                {
     
                    GridDataItem dataitem = (GridDataItem)item;
                    TableCell cell = dataitem["ClientSelectColumn"];
                    CheckBox checkBox = (CheckBox)cell.Controls[0];
                    if (checkBox.Checked)
                    {
                        int userid = Convert.ToInt32(dataitem.GetDataKeyValue("UserId").ToString());
                        if (GroupId != null)
                        {
                            obj.InsertGroupUserCreation(Convert.ToInt32(GroupId), userid);
                        }
                    }
     
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
         
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            DataTable dt = obj.UserDetails();
            RadGrid1.DataSource = dt;
          
        }
     
     
         
    }
    Help needed,
    Thanks
  2. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 16 Aug 2013 Link to this post

    Plz give some quick reply,
    its urgent,
    Thanks
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 16 Aug 2013 Link to this post

    Hi Swapnil,

    Please try the below code snippet.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" PageSize="15" AllowSorting="True" AllowMultiRowSelection="True" OnItemCommand="RadGrid1_ItemCommand" OnPreRender="RadGrid1_PreRender" >
            <MasterTableView DataKeyNames="CustomerID" >
                    <Columns>
                        <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" />
                    </Columns>
           </MasterTableView>
         <ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true" />
    </telerik:RadGrid>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

    C#:
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
      {
          ArrayList selectedItems;
          if (Session["selectedItems"] == null)
          {
              selectedItems = new ArrayList();
          }
          else
          {
              selectedItems = (ArrayList)Session["selectedItems"];
          }
          if (e.CommandName == "RowClick")
          {
              GridDataItem dataItem = (GridDataItem)e.Item;
              string customerID = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["CustomerID"].ToString();
              if (dataItem.Selected)
              {
                  selectedItems.Add(customerID);
                  Session["selectedItems"] = selectedItems;
              }
              else
              {
                  selectedItems.Remove(customerID);
                  Session["selectedItems"] = selectedItems;
              }
          }
          
      }
      protected void RadGrid1_PreRender(object sender, EventArgs e)
      {
          if (Session["selectedItems"] != null)
          {
              ArrayList selectedItems = (ArrayList)Session["selectedItems"];
              Int16 stackIndex;
              for (stackIndex = 0; stackIndex <= selectedItems.Count - 1; stackIndex++)
              {
                  string curItem = selectedItems[stackIndex].ToString();
                  foreach (GridItem item in RadGrid1.MasterTableView.Items)
                  {
                      if (item is GridDataItem)
                      {
                          GridDataItem dataItem = (GridDataItem)item;
                          if (curItem.Equals(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["CustomerID"].ToString()))
                          {
                              dataItem.Selected = true;
                              break;
                          }
                      }
                  }
              }
          }
      }
     
      protected void Button1_Click(object sender, EventArgs e)
      {
          if (Session["selectedItems"] != null)
          {
              ArrayList selectedItems = (ArrayList)Session["selectedItems"];
              Int16 stackIndex;
              for (stackIndex = 0; stackIndex <= selectedItems.Count - 1; stackIndex++)
              {
                  string curItem = selectedItems[stackIndex].ToString();
                  foreach (GridItem item in RadGrid1.MasterTableView.Items)
                  {
                      if (item is GridDataItem)
                      {
                          GridDataItem dataItem = (GridDataItem)item;
                          if (curItem.Equals(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["CustomerID"].ToString()))
                          {
                              
                              dataItem.Selected = true;
                              break;
     
                          }
                      }
                  }
              }
          }    
      }

    Thanks,
    Princy
  5. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 16 Aug 2013 Link to this post

    Thanks Princy,
    Your code is just awesome.
    When i rerun the project the previous run checkbox selection is there where i can reset these checkboxes to get all unchecked checkboxes at each run.
    Thanks
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 16 Aug 2013 Link to this post

    Hi Swapnil,

    Please try setting Session.Clear(),as shown below.

    C#:
    protected void Page_Load(object sender, EventArgs e)
       {
         if (!IsPostBack)
          {
           Session.Clear();
          }
       }

    Thanks,
    Princy
  7. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 16 Aug 2013 Link to this post

    Using session.clear() solved my issue,BUT
    i have 13 records and page size=5 when i check 1 record on each page and then click save only last page i.e.page no 3(current page showing on browser) record will be saved to the database and
    when i changed the page size to 20 then all 3 records will be inserted to the database.
    i used debugger at line
     ArrayList selectedItems = (ArrayList)Session["selectedItems"];
    all record will be listed but only last page records are inserted ,
    Thanks
    CODE:

    if (Session["selectedItems"] != null)
    {
        ArrayList selectedItems = (ArrayList)Session["selectedItems"];
        Int16 stackIndex;
        for (stackIndex = 0; stackIndex <= selectedItems.Count - 1; stackIndex++)
        {
            string curItem = selectedItems[stackIndex].ToString();
            foreach (GridItem item in RadGrid1.MasterTableView.Items)
            {
                if (item is GridDataItem)
                {
                    GridDataItem dataItem = (GridDataItem)item;
                    if (curItem.Equals(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["UserId"].ToString()))
                    {
     
                        dataItem.Selected = true;
                        GroupId = cmbgroupname.SelectedItem.Value.ToString();
                        int userid = Convert.ToInt32(curItem);
                        if (GroupId != null)
                        {
                            obj.InsertGroupUserCreation(Convert.ToInt32(GroupId), userid);
                        }
     
                        break;
     
                    }
                }
            }
        }
    another important issue found with the code is if i checked some recoreds on a page and refresh the page then same records will be added to the database once again
    please reply quickly,
    Thanks
  8. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 19 Aug 2013 Link to this post

    Hi Swapnil,

    Please try the below code snippet to get all the selected rows datakey value on the button click.Rest of the code is same as above.

    C#:
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["selectedItems"] != null)
        {
            ArrayList selectedItems = (ArrayList)Session["selectedItems"];
            Int16 stackIndex;
            for (stackIndex = 0; stackIndex <= selectedItems.Count - 1; stackIndex++)
            {
                RadGrid1.AllowPaging = false;
                RadGrid1.Rebind();
                string curItem = selectedItems[stackIndex].ToString();
                foreach (GridItem item in RadGrid1.MasterTableView.Items)
                {
                    if (item is GridDataItem)
                    {
                        GridDataItem dataItem = (GridDataItem)item;
                        if (curItem.Equals(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["CustomerID"].ToString()))
                        {                          
                            dataItem.Selected = true;
                            Response.Write(curItem);//Datakey value of the selcted items
                            Response.Write("<br/>");
                            //Code To Insert
                            break;
                        }
                    }
                }
            }         
        }
        RadGrid1.AllowPaging = true;
        RadGrid1.Rebind();
    }

    Thanks,
    Princy
  9. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 19 Aug 2013 Link to this post

    its working but the problem is when i click some records then go to the another page then change page size then uncheck some record(means doing anything with grid records) at last when i click save then some records are automaticallya added twice(i think its maintaing state after unchecking records also)
    and
    2)header checkbox select all records and when i refresh all checkboxes are unchecked(not happen in case of normal checkbox)
    Plz
    Thanks
  10. Swapnil
    Swapnil avatar
    96 posts
    Member since:
    Aug 2013

    Posted 19 Aug 2013 Link to this post

    any help
    Thanks
  11. S
    S avatar
    20 posts
    Member since:
    Nov 2014

    Posted 15 Nov 2014 in reply to Swapnil Link to this post

    Hi,

    i have an issue with maintaining state of template column textbox on paging in radgrid.
    I am storing the data in session and then on need datasource event trying to get the data again from session.
    But this does not help.
    Please help me its urgent issue.please refer code below-






    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
        <telerik:RadGrid ID="grChecklist1" runat="server"  AutoGenerateColumns="false"
                onneeddatasource="grChecklist1_NeedDataSource" 
            onpageindexchanged="grChecklist1_PageIndexChanged" >
       <MasterTableView  AllowPaging="true" AutoGenerateColumns="false"  PageSize="5"> 
        <Columns>
        <telerik:GridTemplateColumn DataField="Number" HeaderText="Number" Visible="true">
        <ItemTemplate>
             <asp:TextBox ID="txtNumber" runat="server" Text='<%#Bind("Number")%>'/>
        </ItemTemplate>
       </telerik:GridTemplateColumn>
        <telerik:GridTemplateColumn DataField="TaskDescription" HeaderText="TaskDescription" Visible="true">
        <ItemTemplate>
             <asp:TextBox ID="txtTaskDescription" runat="server" Text='<%#Bind("TaskDescription")%>' />
        </ItemTemplate>
       </telerik:GridTemplateColumn>
       </Columns>

    </MasterTableView>
    </telerik:RadGrid>
       

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    LoadDetails();
                }
            }

            private void LoadDetails()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Number",typeof(string));
                dt.Columns.Add("TaskDescription", typeof(string));
                dt.Rows.Add("1", "task1");
                dt.Rows.Add("2", "task2");
                dt.Rows.Add("3", "task3");
                dt.Rows.Add("4", "task4");
                dt.Rows.Add("5", "task5");
                dt.Rows.Add("6", "task6");
                dt.Rows.Add("7", "task7");
                //grChecklist.DataSource = dt;
               // grChecklist.DataBind();
                Session["dt"] = dt;

            }

            private void RefreshSessionState()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Number", typeof(string));
                dt.Columns.Add("TaskDescription", typeof(string));
                
                foreach (GridDataItem item in grChecklist1.Items)
                {
                  string number=(item.FindControl("txtNumber") as TextBox).Text;
                  string task = (item.FindControl("txtTaskDescription") as TextBox).Text;
                  dt.Rows.Add(number,task);
                }
                if (Session["dt"] != null)
                    Session.Remove("dt");
                Session["dt"] = dt;
            }

           

            protected void grChecklist1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
            {
               // RefreshSessionState();
                grChecklist1.DataSource = (DataTable)Session["dt"];

            }

            protected void grChecklist1_PageIndexChanged(object sender, GridPageChangedEventArgs e)
            {
                grChecklist1.AllowPaging = false;
                grChecklist1.Rebind();
               // RefreshSessionState();
               
                grChecklist1.CurrentPageIndex = e.NewPageIndex;
                grChecklist1.AllowPaging = true;
                grChecklist1.Rebind();
            }
        }
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017