Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Validator for a Datagrid

Status
Not open for further replies.

vwhite

Programmer
Oct 4, 2001
87
AU
Before saving a new record, I need to be able to check if the values in a field in each row of a datagrid add up to the value in another textbox.

I was going to write a Custom Validator function but when I write the following :

<asp:CustomValidator id="cvdFunding" runat="server" ControlToValidate="dgFunding" ErrorMessage="Total must be the same as allocation" Display="Dynamic"/>

I get an error message
"Control 'dgFunding' referenced by the ControlToValidate property of 'cvdFunding' cannot be validated."

Is it not possible to send the whole datagrid to the Validation function so I can run through each row?



....vwhite

"I used to have a handle on life...then it broke
 
Hello again!

I have actually now created a label in the footer of the datagrid that has the total (populated via a client-side script).

So now I am trying to implement a comparevalidator that compares this total within the grid with a textbox outside of the grid.

<asp:comparevalidator CssClass="ERR_STYLE" id="covFunding" runat="server" ErrorMessage="Total must equal Allocation" controltoValidate="lblFundingTotal" ControlToCompare="txtAllocation" Operator="Equal" Display="Dynamic"/>

The validator control is sitting outside the grid, so the comparison isn't possible I suppose because they are in 2 different containers.



....vwhite

"I used to have a handle on life...then it broke
 
It's OK I have solved it. It's not pretty but it works

<asp:customvalidator CssClass="ERR_STYLE" id="cvdFunding" runat="server" ErrorMessage="Total from Funding Sources must equal Allocation" ControlToValidate="txtAllocation" Display="Dynamic" OnServerValidate="cvdFunding_Validate" ClientValidationFunction="cvdFunding_ClientValidate"/>


then the client validation function -


function cvdFunding_ClientValidate(sender, args)
{
var amt = args.value;
var rowcount = document.getElementById('dgFunding').rows.length;
var idname = "dgFunding__ctl" + rowcount + "_lblFundingTotal";
var total = document.getElementById(idname).innerText
if (amt == total)
{
args.isvalid = true;
return;
}
args.IsValid = false;
}

....vwhite

"I used to have a handle on life...then it broke
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top