You are on page 1of 3

function select_deselectall (chkval, idval)

{
var frm = document.forms[0];
// loop through all elements
for (i=0;i< frm.length; i++)
{
// look for our header template's checkbox
if (idval.indexof ('checkall') != -1)
{
// check if main checkbox is checked, then select or deselect datagrid checkboxes
if(chkval == true)
{
frm.elements[i].checked = true;
}
else
{
frm.elements[i].checked = false;
}
// work here with the item template's multiple checkboxes
}
else if (idval.indexof ('deletethis') != -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;
//uncheck main select all checkbox
}
}
}
}
function confirmdelete (frm)
{
// loop through all elements
for (i=0; i<frm.length; i++)
{
// look for our checkboxes only
if (frm.elements[i].name.indexof("deletethis") !=-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)?')
}
}
}
}

<asp:datagrid id="mydatagrid" runat="server" width="700" backcolor="white"


bordercolor="black"
cellpadding="3" cellspacing="0" font-size="9pt" autogeneratecolumns="false"
headerstyle-backcolor="darkred" headerstyle-forecolor="white">
< columns>
< asp:templatecolumn>
< headertemplate>
< asp:checkbox id="checkall" oncheckedchanged="javascript: return select_deselectall
(this.checked,this.id);" runat="server" />
< font face="webdings" color="white" size="4">< /font>
< /headertemplate>
< itemtemplate>
< asp:checkbox id="deletethis" oncheckedchanged="javascript: return select_deselectall
(this.checked, this.id);" runat="server" />
< /itemtemplate>
< /asp:templatecolumn>
< asp:templatecolumn>
< headertemplate>
id
< /headertemplate>
< itemtemplate>
< asp:label id="storeid" text='< %# databinder.eval (container.dataitem, "id") %>'
runat="server"/>
< /itemtemplate>
< /asp:templatecolumn>
< asp:boundcolumn headertext="store" datafield="store" runat="server" />
< asp:boundcolumn headertext="address" datafield="address" runat="server" />
< asp:boundcolumn headertext="city" datafield="city" runat="server" />
< asp:boundcolumn headertext="state" datafield="state" runat="server" />
< asp:boundcolumn headertext="zip" datafield="zip" runat="server" />
< /columns>
< /asp:datagrid>

< asp:button text="delete items" onclick="deletestore" id="confirm" runat="server" />


< span id="outputmsg" runat="server" />

public void deletestore (object sender, eventargs e)


{
string dgids = "";
bool bxschkd = false;
foreach (datagriditem i in mydatagrid.items)
{
checkbox deletechkbxitem = (checkbox) i.findcontrol ("deletethis");
if (deletechkbxitem.checked)
{
bxschkd = true;
// concatenate datagrid item with comma for sql delete
dgids += ((label) i.findcontrol ("storeid")).text.tostring() + ",";
}
}
// set up sql delete statement, using lastindexof to remove tail comma from string.
string deletesql = "delete from stores where stor_id in (" + dgids.substring (0,
dgids.lastindexof (",")) + ")";
if (bxschkd == true)
{
// execute sql query only if checkboxes are checked, otherwise error occurs with initial
null string
try
{
sqlhelper.executenonquery (objconnect, commandtype.text, deletesql);
}
catch (sqlexception err)
{
//exception catching code as usual displaying message
}
//refresh data
binddata();
}
}
public void binddata()
{
string sqlquery = "select stor_id as id, stor_name as store, city, state, zip from stores";
mydatagrid.datasource = sqlhelper.executedataset(objconnect, commandtype.text,
sqlquery);
mydatagrid.databind();
objconnect.close();
objconnect = null;
}