GridView and Validation Controls

A

AlBruAn

I have a GridView on my page that is populated via a call to a function in
another class. Within the GridView, I have three additional columns; one
contains a CheckBox, one contains a TextBox, and the other one contains a
Dropdown list. On the same page and inside the same <Div> as the GridView, I
have a RegularExpressionValidator control and a CustomValidator control, both
of which are disabled in the HTML code. Not knowing any other way of doing
it, I wrote a JavaScript function, entered into on the Submit button's
OnClientClick property, to loop through each row, stopping at the first row
in which the CheckBox is checked. At this point, I'm attempting to set the
TextBox as the ControlToValidate property of the RegExValidator and am
attempting to set the DropDown as being the ControlToValidate of the
CustomValidator control. Despite this and failing to enter anything matching
the RegEx expression in the TextBox and/or failing to select an item from the
DropDown, it appears neither of the validators are working. Page.IsValid
returns true and the function called by the Submit button's OnClick property
contines to execute on the server side.

Following are my two JavaScript validation helpers:
function validateCheckVoucherRequestGrid(val) {
grid =
document.getElementById('ctl00_ContentPlaceHolder1_grdCheckVoucherRequest');
rowCount = grid.rows.length;
var rowCounter;
var index;
for (rowCounter = 1; rowCounter < rowCount; rowCounter++) {
index = rowCounter;
index += 1;
var fieldName =
'ctl00_ContentPlaceHolder1_grdCheckVoucherRequest_ctl0' + index +
'_chkRequest';
var amountField =
'ctl00_ContentPlaceHolder1_grdCheckVoucherRequest_ctl0' + index +
'_txtAmount';
var reasonField =
'ctl00_ContentPlaceHolder1_grdCheckVoucherRequest_ctl0' + index +
'_ddlReasonCode';
if (document.getElementById(fieldName).checked == true) {
if (document.getElementById(amountField).value == "") {
var amountValidator =
document.getElementById('ctl00_ContentPlaceHolder1_revVoucherAmount');
amountValidator.enabled = true;
return false;
break;
}
else
amountValidator.enabled = false;
}
if (document.getElementById(reasonField).text == 'HDRROW') {
var reasonValidator =
document.getElementById('ctl00_ContentPlaceHolder1_cvReasonCode');
reasonValidator.controltovalidate = reasonField;
reasonValidator.enabled = true;
return false;
break;
}
else
reasonValidator.enabled = false;
}
}
}

function checkVoucherRequestReasonIsSelected(val) {
var rowCounter
var index
for (rowCounter = 1; rowCounter < rowCount; rowCounter++) {
index = rowCounter;
index += 1;
var fieldName =
'ctl00_ContentPlaceHolder1_grdCheckVoucherRequest_ctl0' +
index + '_chkRequest';
if (document.getElementById(fieldName).checked == true) {
var reasonField =
= 'ctl00_ContentPlaceHolder1_grdCheckVoucherRequest_ctl0' +
index
+ '_ddlReasonCode';
if (document.getElementById(reasonField).text == 'HDRROW') {
var reasonValidator = document.getElementById
('ctl00_ContentPlaceHolder1_cvReasonCode');
reasonValidator.controltovalidate = reasonField;
reasonValidator.enabled = true;
return false;
break;
}
}
}
}

Pertinent portions of my HTML are as follows:
<asp:TemplateField HeaderText="Voucher Amount" ControlStyle-Width="70px">
<ItemTemplate>
<asp:TextBox ID="txtAmount" runat="server" />
<ajaxToolkit:MaskedEditExtender ID="meeVoucherAmount"
runat="server" Mask="999,999.99" MaskType="Number"
TargetControlID="txtAmount" InputDirection="RightToLeft"
PromptCharacter=" " />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="70px" />
</asp:TemplateField>
<asp:TemplateField headertext="Voucher Reason">
<ItemTemplate >
<asp:DropDownList id="ddlReasonCode" runat="server" Width="300px">
</asp:DropDownList>
</ItemTemplate>
<ItemStyle Width="300px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Request Voucher">
<ItemTemplate>
<asp:CheckBox ID="chkRequest" Checked="false" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="70px" />
</asp:TemplateField>

<asp:RegularExpressionValidator ID="revVoucherAmount" runat="server"
EnableClientScript="true" Enabled="false" ValidationExpression=
"^\d+(\.\d\d)?$" ControlToValidate="txtToDate" ErrorMessage="Please enter
Voucher Amount requested for any selected Voucher Request(s) submitted."
/>
<asp:CustomValidator ID="cvReasonCode" runat="server"
EnableClientScript="true" Enabled="false" ControlToValidate="txtToDate"
ClientValidationFunction="checkVoucherRequestReasonIsSelected();"
ErrorMessage="Please select a Request Reason for any Voucher Request(s)
submitted." />
<asp:Button ID="btnSubmitRequests" runat="server" Text="Submit Request(s)"
OnClientclick="validateCheckVoucherRequestGrid();"
OnClick="SubmitRequests_Click" />

My code-behind is as follows:
Protected Sub SubmitRequests_Click(ByVal sender As Object, ByVal e As
EventArgs)
Dim requestGrid As GridView = CType(grdCheckVoucherRequest, GridView)
Dim retVal As Boolean = True

If Page.IsValid Then
' Process data in grid and store it in the database
End If
End Sub

As I see it, this checks only the first row in which the CheckBox is checked
which would result in it being the only row checked; any other rows within
the GridView whose CheckBox may be checked will never be verified. What is
there I can do to ensure all rows that are checked have TextBox entries
matching the RegEx expression for them and that all of them also have a
selection made from the corresponding DropDown list?

Many thanks!

Allen B. Anderson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top