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();




}




}

Friday, May 2, 2008

JavaScript function for currency check with two decimal

function IntDec()
{
var name = document.getElementById('TextBox1').value;

if (name.indexOf(".") != -1 )// First check text box contain period or not
{
var digitonly = /^[-]?\d*\.?\d*$/;
var arrString = name.split('.');
if(arrString.length == 2)// check if user enter one or more than one
decimal
{
if(arrString[1].length ==2)// chk if user enter more than 2 decimal
{
var intPart = arrString[0];
if(intPart.length == 0)
{
alert('Amount is very low');
document.getElementById('TextBox1').value= '00.'+ arrString
}

if(!intPart.match(digitonly)) // check that all inputs are
digit or not
{
alert('Must Enter Digit')
document.getElementById('TextBox1').value="";
document.getElementById('TextBox1').focus();
return false;
}// end of chekc alll inputs are digit or not


var decimalPart = arrString[1];
if(!decimalPart.match(digitonly)) // check that all inputs are
digit or not
{
alert(decimalPart);
alert('Must Enter Digit')
document.getElementById('TextBox1').value="";
document.getElementById('TextBox1').focus();
return false;
}// end of chekc alll inputs are digit or not



}
else
{
if(!name.match(digitonly)) // check that all inputs are digit or not
{
alert('Must Enter Digit')
document.getElementById('TextBox1').value="";
document.getElementById('TextBox1').focus();
return false;
}// end of chekc alll inputs are digit or not


alert('Please Enter 2 digit decimal');


document.getElementById('TextBox1').value = arrString[0]+'.00';
document.getElementById('TextBox1').focus();
return false;
}// end chk if user enter more than 2 decimal
}
else // if user enter one or more than one decimal
{
alert('Please Enter Signle Periond');
document.getElementById('TextBox1').value="";
document.getElementById('TextBox1').focus();
return false;

} // end of user enter one or more than one decimal
}// end of chekc user enter period or not
else
{

var digitonly2 = /^[-]?\d*\.?\d*$/;
//document.getElementById('TextBox1').value="";
if(!name.match(digitonly2)) // check that all inputs are digit or not
{
alert('Must Enter Digit')
document.getElementById('TextBox1').value="";
document.getElementById('TextBox1').focus();
return false;
}// end of chekc alll inputs are digit or not

alert('Enter corrrect format e.g. 0000000000.00 ');

document.getElementById('TextBox1').value +='.00';
return false;
}

return true;

}