GridView and Validation Controls

Discussion in 'ASP .Net Web Controls' started by AlBruAn, Oct 10, 2007.

  1. AlBruAn

    AlBruAn Guest

    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
     
    AlBruAn, Oct 10, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    14
    Views:
    4,108
    Chad Z. Hower aka Kudzu
    Jan 30, 2004
  2. =?Utf-8?B?ZGh1cndpdHo=?=
    Replies:
    1
    Views:
    463
    Teemu Keiski
    Apr 10, 2004
  3. baroque Chou
    Replies:
    4
    Views:
    435
    Peter Blum
    Jan 24, 2006
  4. Nathan Sokalski
    Replies:
    4
    Views:
    1,149
    Muhammad Naveed Yaseen
    Sep 5, 2007
  5. Kat

    Gridview & Validation Controls

    Kat, Apr 9, 2007, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    1,065
    Teemu Keiski
    Apr 9, 2007
Loading...

Share This Page