Javascript Validation in Gridview

Make function in “Java Script” as Below… grvPowderCoat is Gridview ID

function PowderCoatValidate()
{
var result=””;
var GrvTst = document.getElementById(GetClientId(“grvPowderCoat”));
if (GrvTst != null) {

var Inputs = GrvTst.getElementsByTagName(“input”);
for (i = 0; i < Inputs.length; i++)
{
if (Inputs[i].type == 'text') {
var test1 = Inputs[i].id;
var array1 = test1.split("_");
if (array1[array1.length-1] == 'txtPowderA') {

if (Inputs[i].value == "")
{
result += "- Enter A\n";
//alert("Enter Width ");
//return false;
}
if(isNaN(Inputs[i].value))
{
result+= "- Enter Valid A\n";
}
}
if (array1[array1.length-1] == 'txtPowderValue') {

if (Inputs[i].value == "")
{
result += "- Enter Total\n";
}
if(isNaN(Inputs[i].value))
{
result+= "- Enter Valid Total\n";
}
}
}
}
var DDL = GrvTst.getElementsByTagName("select");
for (i = 0; i < DDL.length; i++)
{
if (DDL[i].type == 'select-one')
{
var test1 = DDL[i].id;
var array1 = test1.split("_");
if (array1[array1.length-1] == 'ddlPowderId')
{
if (DDL[i].selectedIndex == 0)
{
result += "- Select Section\n";
}
}
}
}
if(result!="")
{
alert(result);
return false;
}
}
}

Call this function in .CS Page

protected void grvPowderCoat_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState ==
DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
if (e.Row.RowType == DataControlRowType.DataRow) //check for RowType
{
string id = e.Row.Cells[0].Text; // Get the id to be deleted
LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[2]; //cast the ShowDeleteButton link to linkbutton
if (lb != null)
{
lb.Attributes.Add(“onclick”, “return ConfirmOnDelete(‘” + id + “‘);”); //attach the JavaScript function with the ID as the paramter
}
}
}
if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
{
if (e.Row.RowType == DataControlRowType.DataRow) //check for RowType
{
string id = e.Row.Cells[0].Text; // Get the id to be deleted
LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[0]; //cast the ShowDeleteButton link to linkbutton
if (lb != null)
{
lb.Attributes.Add(“onclick”, “return PowderCoatUpdate();”); //attach the JavaScript function with the ID as the paramter
}
}
}
}

Share