Wednesday, May 7, 2008

Delete Reocrds by check all check box from gridView datagrid datalist

Delete Reocrds by check all check box from gridView datagrid datalist

This will give you a list of all the fields in the record. At the top a check box for select/unselect button and eache row

contain check box for specified selection and bottom of the list is a DELETE button.






[script language="javascript" type="text/javascript"]
function check_uncheck(Val)
{
var ValChecked = Val.checked;
var ValId = Val.id;
var frm = document.forms[0];
// Loop through all elements
for (i = 0; i [ frm.length; i++)
{
// Look for Header Template's Checkbox
//As we have not other control other than checkbox we just check following statement
if (this != null)
{
if (ValId.indexOf('CheckAll') != - 1)
{
// Check if main checkbox is checked,
// then select or deselect datagrid checkboxes
if (ValChecked)
frm.elements[i].checked = true;
else
frm.elements[i].checked = false;
}
else if (ValId.indexOf('deleteRec') != - 1)
{
// Check if any of the checkboxes are not checked, and then uncheck top select all checkbox
if (frm.elements[i].checked == false)
frm.elements[1].checked = false;
}
} // if
} // for
} // function
//Delete confirmation Function
function confirmMsg(frm)
{
// loop through all elements
for (i = 0; i [ frm.length; i++)
{
// Look for our checkboxes only
if (frm.elements[i].name.indexOf("deleteRec") != - 1)
{
// If any are checked then confirm alert, otherwise nothing happens
if (frm.elements[i].checked)
return confirm('Are you sure you want to delete your selection(s)?')
}
}
}

[/script]

HTML part

[asp:GridView ID="GridViewNewsDelete" DataKeyNames="NewsId" runat="server" ]
[Columns]
[asp:TemplateField]
[HeaderTemplate]
[asp:CheckBox ID="CheckAll" onclick="return check_uncheck (this );" runat="server" /][/HeaderTemplate]
[ItemTemplate]
[asp:Label ID="NewsId" Visible="false"
Text='[%# DataBinder.Eval (Container.DataItem, "NewsId") %]'
runat="server" /]
[asp:CheckBox ID="deleteRec" onclick="return check_uncheck (this );"
runat="server" /][/ItemTemplate]
[/asp:TemplateField]
[/Columns]
[/asp:GridView]


[asp:Button ID="Button1" runat="server" OnClientClick="return confirmMsg(this.form)"
Text="Button" OnClick="Button1_Click" /]



C# coding

if (!IsPostBack)
{
BindGridView();
}



protected void Button1_Click(object sender, EventArgs e)
{
string gvIDs = "";
bool chkBox = false;
//'Navigate through each row in the GridView for checkbox items
foreach (GridViewRow gv in GridViewNewsDelete.Rows)
{
CheckBox deleteChkBxItem = (CheckBox)gv.FindControl("deleteRec");
if (deleteChkBxItem.Checked)
{
chkBox = true;
// Concatenate GridView items with comma for SQL Delete
gvIDs += ((Label)gv.FindControl("NewsId")).Text.Trim();
NewsId = Convert.ToInt32(gvIDs);

// call delete function

MiddleLayerFunctions.deleteNewsWithoutImage("deleteNewsWithoutImage", NewsId);
}
}
if (chkBox)
{
// OR Execute SQL Query only if checkboxes are checked to avoid any error with initial null string




MiddleLayerFunctions.deleteNewsWithoutImage("deleteNewsWithoutImage", NewsId);




BindGridView();




}




}

No comments: