mary jean ligas

mary jean ligas

  • NA
  • 420
  • 196.2k

How to make fast saving Checkboxes inside Gridview

Jan 7 2013 4:46 AM

Scenario: I have a gridview that loads morethan 300 rows..but it takes a minute to save the value of checkboxes..how can i resolved this..





ASPX

 

<asp:GridView ID="gridRolApp" runat="server" ClientIDMode="Static"

                BorderColor="#0070C0" GridLines="None" AutoGenerateColumns="False"

                Width="1100px" UseAccessibleHeader="false" CssClass="Content"

                datakeynames="User_Pidx,BusinessUnit,Department">

             <Columns>

                <asp:TemplateField >

                   <HeaderStyle Width="200px"/>

                 <HeaderTemplate>BUSINESS UNIT</HeaderTemplate>

                       <ItemStyle BackColor="#EFEFEF" Width="200px" Font-Size="12px"/>

                       <ItemTemplate>

                         <%#Eval("Plant_Name")%>                   

                       </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField>

                <HeaderStyle Width="200px"/>

                   <HeaderTemplate>DEPARTMENT</HeaderTemplate>

                      <ItemStyle BackColor="#EFEFEF" Width="200px" Font-Size="12px"/>

                       <ItemTemplate>

                         <%#Eval("Dept_Name")%>

                       </ItemTemplate>

                </asp:TemplateField>

                 <asp:TemplateField>

                   <HeaderStyle HorizontalAlign="Center" />

                   <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllDeptHead" AutoPostBack="True" oncheckedchanged="chkAllDeptHead_CheckedChanged" />

                   <br>DH

                   </HeaderTemplate>

                   <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkDeptHead" OnDataBinding="chkDeptHead_DataBinding"/></center>

                   </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                   <HeaderStyle HorizontalAlign="Center" />

                   <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllSM" AutoPostBack="True" oncheckedchanged="chkAllSM_CheckedChanged" />

                   <br>SM

                   </HeaderTemplate>

                   <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkSM" OnDataBinding="chkSM_DataBinding"/></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                   <HeaderTemplate>

                   <HeaderStyle HorizontalAlign="Center" />

                   <asp:CheckBox runat="server" id="chkAllTMM" AutoPostBack="True" oncheckedchanged="chkAllTMM_CheckedChanged" />

                   <br>TMM

                   </HeaderTemplate>

                        <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkTMM" OnDataBinding="chkTMM_DataBinding" /></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                  <HeaderStyle HorizontalAlign="Center" />

                  <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllHRM" AutoPostBack="True" oncheckedchanged="chkAllHRM_CheckedChanged" />

                   <br />HRM

                   </HeaderTemplate>

                   <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkHRM" OnDataBinding="chkHRM_DataBinding"/></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                   <HeaderStyle HorizontalAlign="Center" />

                   <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllFM" AutoPostBack="True" oncheckedchanged="chkAllFM_CheckedChanged" />

                   <br />FM

                   </HeaderTemplate>

                   <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkFM" OnDataBinding="chkFM_DataBinding"/></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                   <HeaderStyle HorizontalAlign="Center" />

                   <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllFD" AutoPostBack="True" oncheckedchanged="chkAllFD_CheckedChanged" />

                   <br />FD

                   </HeaderTemplate>

                   <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkFD" OnDataBinding="chkFD_DataBinding" /></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                   <HeaderStyle HorizontalAlign="Center" />

                   <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllGM" AutoPostBack="True" oncheckedchanged="chkAllGM_CheckedChanged" />

                   <br />GM

                   </HeaderTemplate>

                   <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkGM" OnDataBinding="chkGM_DataBinding" /></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                   <HeaderStyle HorizontalAlign="Center" />

                   <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllSGM" AutoPostBack="True" oncheckedchanged="chkAllSGM_CheckedChanged" />

                   <br />SGM

                   </HeaderTemplate>

                       <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkSGM" OnDataBinding="chkSGM_DataBinding"/></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                   <HeaderStyle HorizontalAlign="Center" />

                   <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllFC" AutoPostBack="True" oncheckedchanged="chkAllFC_CheckedChanged" />

                   <br />FC

                   </HeaderTemplate>

                       <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkFC" OnDataBinding="chkFC_DataBinding" /></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                   <HeaderStyle HorizontalAlign="Center" />

                   <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllVPFHead" AutoPostBack="True" oncheckedchanged="chkAllVPFHead_CheckedChanged" />

                   <br />VP-FH

                   </HeaderTemplate>

                       <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkVPFHead" OnDataBinding="chkVPFHead_DataBinding" /></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                  <HeaderStyle HorizontalAlign="Center" />

                  <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllVPML" AutoPostBack="True" oncheckedchanged="chkAllVPML_CheckedChanged" />

                   <br />VP-M&L

                   </HeaderTemplate>

                       <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkVPML" OnDataBinding="chkVPML_DataBinding" /></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                   <HeaderStyle HorizontalAlign="Center" />

                   <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllCorpLegal" AutoPostBack="True" oncheckedchanged="chkAllCorpLegal_CheckedChanged" />

                   <br />CLS

                   </HeaderTemplate>

                       <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkCorpLegal" OnDataBinding="chkCorpLegal_DataBinding"/></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                  <HeaderStyle HorizontalAlign="Center" />

                  <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllVPHR" AutoPostBack="True" oncheckedchanged="chkAllVPHR_CheckedChanged" />

                   <br />VP-HR

                   </HeaderTemplate>

                       <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkVPHR" OnDataBinding="chkVPHR_DataBinding" /></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                   <HeaderStyle HorizontalAlign="Center" />

                   <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllEVPSales" AutoPostBack="True" oncheckedchanged="chkAllEVPSales_CheckedChanged" />

                   <br />EVPSales

                   </HeaderTemplate>

                       <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkEVPSales" OnDataBinding="chkEVPSales_DataBinding" /></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                  <HeaderStyle HorizontalAlign="Center" />

                  <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllCFO" AutoPostBack="True" oncheckedchanged="chkAllCFO_CheckedChanged" />

                   <br />CFO

                   </HeaderTemplate>

                       <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkCFO" OnDataBinding="chkCFO_DataBinding"  /></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                 <asp:TemplateField>

                   <HeaderStyle HorizontalAlign="Center" />

                   <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllPres" AutoPostBack="True" />

                   <br />Pres

                   </HeaderTemplate>

                       <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkPres" OnDataBinding="chkPres_DataBinding"/></center>

                       </ItemTemplate>

                 </asp:TemplateField>

                  <asp:TemplateField>

                  <HeaderStyle HorizontalAlign="Center" />

                  <HeaderTemplate>

                   <asp:CheckBox runat="server" id="chkAllCEO" AutoPostBack="True" oncheckedchanged="chkAllCEO_CheckedChanged" />

                   <br />CEO

                   </HeaderTemplate>

                       <ItemTemplate>

                         <center><asp:CheckBox runat="server" id="chkCEO" OnDataBinding="chkCEO_DataBinding"/></center>

                       </ItemTemplate>

                 </asp:TemplateField>

          </Columns>

          <HeaderStyle CssClass="GridviewScrollHeader" />

          <RowStyle CssClass="GridviewScrollItem" />

          <PagerStyle CssClass="GridviewScrollPager" />     

          </asp:GridView>

 

<asp:Button runat="server" Text="Save" ID="btnSave" Width="100" CssClass="TextDrop" onclick="btnSave_Click" />

 

 

protected void btnSave_Click(object sender, EventArgs e)

        {

            if (txtValidation().Equals(true))

            {

                CLMSDataContext context = new CLMSDataContext();

                try

                {

                    context.ExecuteCommand("UPDATE Dim_User SET PosLoc_Abbreviation = '" + txtPosLocAbb.Text + "', " + Environment.NewLine +

                                            " Active ='"+ chkActive.Checked +"'," + Environment.NewLine +

                                            " Admin ='" + chkAdmin.Checked + "'," + Environment.NewLine +

                                            " RealTime_Notification ='" + ChkRTNotification.Checked + "'," + Environment.NewLine +

                                            " LastName = '" + txtLastName.Text + "', " + Environment.NewLine +

                                            " FirstName = '" + txtFirstName.Text + "', " + Environment.NewLine +

                                            " MiddleInitial = '" + txtMI.Text.Trim() + "', " + Environment.NewLine +

                                            " NickName = '" + txtAKA.Text + "', " + Environment.NewLine +

                                            " SMTPAddress = '" + txtEmail.Text + "', " + Environment.NewLine +

                                            " Plant_PIdx = '" + defaultBU.SelectedValue + "', Dept_PIdx = '" + defaultDept.SelectedValue + "' " + Environment.NewLine +

                                            " WHERE User_Idx = '" + Convert.ToInt64(Session["UserID"]) + "'");

 

                        bool chkboxIsChecked = false;

 

                        List<Dim_User_Role> deleteRole = (from key in context.Dim_User_Roles

                                                          where key.User_PIdx == Convert.ToInt64(Session["UserID"])

                                                          select key).ToList();

 

                        context.Dim_User_Roles.DeleteAllOnSubmit(deleteRole);

                        context.SubmitChanges();

 

                        foreach (GridViewRow row in gridRolApp.Rows)

                        {

 

                            string pUserID = gridRolApp.DataKeys[row.RowIndex].Values["User_Pidx"].ToString();

                            string pPlantID = gridRolApp.DataKeys[row.RowIndex].Values["BusinessUnit"].ToString();

                            string pDeptID = gridRolApp.DataKeys[row.RowIndex].Values["Department"].ToString();

 

                            chkboxIsChecked = false;

                            CheckBox chkDeptHead = (CheckBox)row.FindControl("chkDeptHead");

                            if ((chkDeptHead != null) && (chkDeptHead.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkDeptHead"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkSM = (CheckBox)row.FindControl("chkSM");

                            if ((chkSM != null) && (chkSM.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkSM"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkTMM = (CheckBox)row.FindControl("chkTMM");

                            if ((chkTMM != null) && (chkTMM.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkTMM"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkHRM = (CheckBox)row.FindControl("chkHRM");

                            if ((chkHRM != null) && (chkHRM.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkHRM"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkFM = (CheckBox)row.FindControl("chkFM");

                            if ((chkFM != null) && (chkFM.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkFM"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkFD = (CheckBox)row.FindControl("chkFD");

                            if ((chkFD != null) && (chkFD.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkFD"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkGM = (CheckBox)row.FindControl("chkGM");

                            if ((chkGM != null) && (chkGM.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkGM"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkSGM = (CheckBox)row.FindControl("chkSGM");

                            if ((chkSGM != null) && (chkSGM.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkSGM"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkFC = (CheckBox)row.FindControl("chkFC");

                            if ((chkFC != null) && (chkFC.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkFC"), chkboxIsChecked);

                           

                            chkboxIsChecked = false;

                            CheckBox chkVPFHead = (CheckBox)row.FindControl("chkVPFHead");

                            if ((chkVPFHead != null) && (chkVPFHead.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkVPFHead"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkVPML = (CheckBox)row.FindControl("chkVPML");

                            if ((chkVPML != null) && (chkVPML.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkVPML"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkCorpLegal = (CheckBox)row.FindControl("chkCorpLegal");

                            if ((chkCorpLegal != null) && (chkCorpLegal.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkCorpLegal"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkVPHR = (CheckBox)row.FindControl("chkVPHR");

                            if ((chkVPHR != null) && (chkVPHR.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkVPHR"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkEVPSales = (CheckBox)row.FindControl("chkEVPSales");

                            if ((chkEVPSales != null) && (chkEVPSales.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkEVPSales"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkCFO = (CheckBox)row.FindControl("chkCFO");

                            if ((chkCFO != null) && (chkCFO.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkCFO"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkPres = (CheckBox)row.FindControl("chkPres");

                            if ((chkPres != null) && (chkPres.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkPres"), chkboxIsChecked);

 

                            chkboxIsChecked = false;

                            CheckBox chkCEO = (CheckBox)row.FindControl("chkCEO");

                            if ((chkCEO != null) && (chkCEO.Checked == true))

                            {

                                chkboxIsChecked = true;

                            }

                            RoleSaving(Convert.ToInt64(pUserID), Convert.ToInt64(pPlantID), Convert.ToInt64(pDeptID), getRole("chkCEO"), chkboxIsChecked);

 

                           

                        }

 

                        successTitle.InnerText = "User Registration";

                        pSuccessMessage1.InnerText = "User successfully registered!";

                        btnSuccessOk.Focus();

                        modpopSuccess.Show();

 

                }

                catch (Exception ex)

                {

                    successTitle.InnerText = "User Registration";

                    pSuccessMessage1.InnerText = ex.Message;

                    btnSuccessOk.Focus();

                    modpopSuccess.Show();

 

                }

                finally

                { context.Dispose(); }

            }

            else

            {

                successTitle.InnerText = "User Registration";

                pSuccessMessage1.InnerText = "Incomplete field!";

                btnSuccessOk.Focus();

                modpopSuccess.Show();

               

            }

        }

 

 

      private Int64 getRole(string RoleStr)

        {

            Int64 roleid = 0;

 

            switch (RoleStr)

            {

                //case "chkInitiator": roleid = 1; break;

                case "chkDeptHead": roleid = 2; break;

                case "chkSM": roleid = 3; break;

                case "chkTMM": roleid = 4; break;

                case "chkHRM": roleid = 5; break;

                case "chkFM": roleid = 6; break;

                case "chkFD": roleid = 7; break;

                case "chkGM": roleid = 8; break;

                case "chkSGM": roleid = 9; break;

                case "chkFC": roleid = 10; break;

                case "chkVPFHead": roleid = 11; break;

                case "chkVPML": roleid = 12; break;

                case "chkCorpLegal": roleid = 13; break;

                case "chkVPHR": roleid = 14; break;

                case "chkEVPSales": roleid = 15; break;

                case "chkCFO": roleid = 16; break;

                case "chkPres": roleid = 17; break;

                case "chkCEO": roleid = 18; break;

                default: roleid = 0; break;

            }

            return roleid;

        }

 

      

       

        private void RoleSaving(Int64 userid, Int64 plant, Int64 dept, Int64 role, bool isChecked)

        {

            CLMSDataContext context = new CLMSDataContext();

            try

            {

                var roleRec = (from r in context.Dim_User_Roles

                               where r.User_PIdx == userid &&

                               r.Dept_PIdx == dept &&

                               r.Plant_PIdx == plant &&

                               r.Role_PIdx == role

                               select r).ToList();

 

                if (roleRec.Count == 0)

                {

                    try

                    {

                        if (isChecked == true)

                        {

                            Dim_User_Role uRol = new Dim_User_Role();

                            uRol.User_PIdx = userid;

                            uRol.Plant_PIdx = plant;

                            uRol.Dept_PIdx = dept;

                            uRol.Role_PIdx = role;

 

                            context.Dim_User_Roles.InsertOnSubmit(uRol);

                            context.SubmitChanges();

 

                        }

                    }

                    catch (Exception e)

                    {

                        context.Transaction.Rollback();

                        Response.Write(e.Message);

                    }

                }

 

            }

            catch (Exception e)

            { Response.Write(e.Message); return; }

            finally

            {

                context.Dispose();

            }

 

        }