Thursday, September 4, 2008

Script for check boxes All check & Un Check

This is the script for CheckBox Checked all in the datalist or Repeater like gmail.

call this method on pageLoad.


private void LoadScript()

{
string strScript;
strScript = " ";
if (!this.IsClientScriptBlockRegistered("clientScriptCheckAll"))
{
this.RegisterClientScriptBlock("clientScriptCheckAll", strScript);
}
strScript = "";
strScript = "<" + "script language" + "=" + "JavaScript" + "> ";
strScript += "function CheckChanged() ";
strScript += "{ ";
strScript += " var frm = document.aspnetForm; ";
strScript += " var boolAllChecked; ";
strScript += " boolAllChecked=true; ";
strScript += " for(i=0;i<>
strScript += " { ";
strScript += " e=frm.elements[i]; ";
strScript += " if ( e.type=='checkbox' && e.name.indexOf('Id') != -1 )";
strScript += " if(e.checked== false) ";
strScript += " { ";
strScript += " boolAllChecked=false; ";
strScript += " break; ";
strScript += " } ";
strScript += " } ";
strScript += " for(i=0;i<>
strScript += " { ";
strScript += " e=frm.elements[i]; ";
strScript += " if ( e.type=='checkbox' && e.name.indexOf('checkAll') != -1 )";
strScript += " { ";
strScript += " if( boolAllChecked==false) ";
strScript += " e.checked= false ; ";
strScript += " else ";
strScript += " e.checked= true; ";
strScript += " break; ";
strScript += " } ";
strScript += " } ";
strScript += " } ";
strScript += " ";
if (!this.IsClientScriptBlockRegistered("clientScriptCheckChanged"))
{
this.RegisterClientScriptBlock("clientScriptCheckChanged", strScript);
}
}


In DataList Header has the HTML checkBox which has the onclick="CheckAll(this)" when we check the header checkbox all the check boxes will checked & when You uncheck the checkbox all the check box will unchecked.


asp:DataList ID="dlstComment" runat="server" Width="100%" CellSpacing=2
DataKeyField="CommentID" onitemcommand="dlstComment_ItemCommand">
HeaderTemplate>
tr>
td >
input type="checkbox" id="checkAll" onclick="CheckAll(this);" runat="server" name="checkAll">Select All
/td>
td>Employee ID
td >Name
td>Email Address
td>Address

/tr>
/HeaderTemplate>
ItemTemplate>
tr>
td>
input type="checkbox" runat="server" id="chxId" onclick="CheckChanged();" checked='false' name="EmpId" />
/td>
td><%#Eval("EmpID") %>
td ><%#Eval("Name")%>
td><%#Eval("EmailAddress")%>
td><%#Eval("Address")%>
/tr>
/ItemTemplate>
/asp:DataList>

comform message in datalist & Repeter

Use this code in the Datalist or Repeter itemDataBound Event....

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{

ImageButton db = (ImageButton)e.Item.FindControl("imgbtnDelete");

// Assign the Delete button's OnClientClick property
db.OnClientClick = string.Format("return confirm('Are you sure you want to delete this topic?');");
}