Breaking News

Editors Picks

Thursday, December 15, 2011

Delete Multiple Rows Records Gridview CheckBox Confirmation in ASP.NET


<script type="text/javascript">
 function SelectAllCheckboxes(cbSelectAll)
 {
var gvcheck = document.getElementById('gvuseredit');
var i;
if (cbSelectAll.checked)
{
for (i = 0; i < gvcheck.rows.length-1; i++)
{
var inputs = gvcheck.rows[i].getElementsByTagName('input');
inputs[0].checked = true;
}
      }
      else
      {
            for (i = 0; i < gvcheck.rows.length-1; i++)
{
var inputs = gvcheck.rows[i].getElementsByTagName('input');
inputs[0].checked = false;
}
      }
}
function Selectchildcheckboxes(header)
{
var ck = header;
var count = 0;
var gvcheck = document.getElementById('gvuseredit');
var headerchk = document.getElementById(header);
var rowcount = gvcheck.rows.length;
for (i = 1; i < gvcheck.rows.length-1; i++)
{
var inputs = gvcheck.rows[i].getElementsByTagName('input');
if (inputs[0].checked)
 {
  count++;
       }
       }
if (count == rowcount-1)
{
 cbSelectAll.checked = true;
}
else
{
 cbSelectAll.checked = false;
}
}

function DeleteConfirmation()
{
 if (confirm('Are you sure, you want to delete selected records ?')==true)
 return true;
 else
 return false;
}
script>

<asp:GridView ID="gvuseredit" runat="server" AllowSorting="true" AutoGenerateColumns="false"
AllowPaging="true" ShowFooter="true"
onrowdatabound="gvuseredit_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Select User">
<HeaderTemplate>
<asp:CheckBox ID="cbSelectAll" runat="server" onclick="javascript:SelectAllCheckboxes(this)" Text="Select All" />
HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkbdeleteuser" runat="server" />
ItemTemplate>
<FooterTemplate>
<asp:Button ID="btndeleteUser" runat="server" Text="Delete"
onclick="btndeleteUser_Click" OnClientClick="return DeleteConfirmation();"  />FooterTemplate>
<HeaderStyle Width="" />
asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="True" CommandName="Update"
Text="Update">asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">asp:LinkButton>
EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit">asp:LinkButton>
ItemTemplate>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
asp:TemplateField>
<asp:TemplateField HeaderText="rec ID">
<ItemTemplate>
<asp:Label ID="lblrecID" runat="server" Text='<%# Bind("recID") %>'>asp:Label>
ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtrecID" runat="server" Text='<%# Bind("recID") %>'>asp:Label>
EditItemTemplate>
<HeaderStyle Width="" />
asp:TemplateField>
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server" Text='<%# Bind("UserName") %>'>asp:Label>
ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblUserName" runat="server" Text='<%# Bind("UserName") %>'>asp:Label>
EditItemTemplate>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="lblpwd" runat="server" Text='<%# Bind("pwd") %>'>asp:Label>
ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lbpwd" runat="server" Text='<%# Bind("pwd") %>'>asp:Label>
EditItemTemplate>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
asp:TemplateField>
Columns>
asp:GridView>





protected void gvuseredit_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox headerchk =    (CheckBox)gvuseredit.HeaderRow.FindControl("cbSelectAll");
CheckBox childchk = (CheckBox)e.Row.FindControl("chkbdeleteuser");
childchk.Attributes.Add("onclick", "javascript:Selectchildcheckboxes('" + ((System.Web.UI.Control)(headerchk)).ClientID + "')");
}
}


private void DeleteMultipleRecords(StringCollection idCollection)
{
string IDs = "";
foreach (string id in idCollection)
{
IDs += id.ToString() + ",";
}
try
{
string test = IDs.Substring(0, IDs.LastIndexOf(","));
string sql = "Delete from useraccessprofile WHERE recid in (" + test + ")";
glbc.ExecuteQuery(sql);
}
catch (Exception ex)
{
string errorMsg = "Error in Deletion";
errorMsg += ex.Message;
throw new Exception(errorMsg);
}
finally
{
//con.Close();
}
}
protected void btndeleteUser_Click(object sender, EventArgs e)
{

StringCollection idCollection = new StringCollection();
string strID = string.Empty;
for (int i = 0; i < gvuseredit.Rows.Count; i++)
{
CheckBox chkDelete = (CheckBox)gvuseredit.Rows[i].Cells[0].FindControl("chkbdeleteuser");
if (chkDelete != null)
{
if (chkDelete.Checked)
{
//strID = gvuseredit.Rows[i].Cells[2].Text;
strID = ((Label)gvuseredit.Rows[i].Cells[2].Controls[1]).Text;
idCollection.Add(strID);
}
}
}
if (idCollection.Count > 0)
{

DeleteMultipleRecords(idCollection);
gvuseredit.DataBind();
}
else
{
//lblMessage.Text = "Please select any row to delete";
}
}
Read more ...

Contact Us

Name

Email *

Message *