Mahe
Mahe

Reputation: 1432

how to enable disable validators based on page controls?

I have a gridView as follows.

<asp:GridView ID="grvLocationCash" runat="server" AutoGenerateColumns="false" CssClass="gridtable" DataKeyNames="LocationId">
    <Columns>
        <asp:BoundField HeaderText="Location Name" DataField="LocationName" />
        <asp:TemplateField HeaderText="Amount">
            <ItemTemplate>
                <asp:TextBox ID="txtAmount" runat="server" CssClass="txtbox" Visible="true"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvAmount" runat="server" ControlToValidate="txtAmount" ErrorMessage="Please enter the Amount" Display="Dynamic" SetFocusOnError="true"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="rgvAmount" runat="server" ControlToValidate="txtAmount" ValidationExpression="^\d+$" ErrorMessage="Please enter whole numbers only" Display="Dynamic" SetFocusOnError="true"></asp:RegularExpressionValidator>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

and a save button with the following code.

  <asp:Button ID="btnLocationAmountList" runat="server" OnClick="btnLocationAmountList_Click" Text="Save" Width ="100px" />

after the gridview is bound, there are about 20 rows in it. When I type in numbers into the text box of the first row and press Enter key to go to second text box, the Required Field Validator is triggered for all the other 19 rows.

But, if I type numbers into the first text box and mouse click on the next text box, the RequiredFieldValidator is not triggered.

The page must be validated only if the Save button is clicked. Values must be entered for all the textboxes before save, and all the values must be integer only. (11,125,6589 etc). How to enable disable validators for a particular control?

UPDATE: here is the page_load event code in my class file.

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGrid();
            }
        }


        protected void BindGrid()
        {
            FinanceServiceRef.FinanceServiceClient obj = new FinanceServiceRef.FinanceServiceClient("WSHttpBinding_IFinanceService");
            var ds = obj.ViewLocationAmountCashManagement();
            grvLocationCash.DataSource = ds;
            grvLocationCash.DataBind();
        }

Upvotes: 2

Views: 1036

Answers (2)

Irfan TahirKheli
Irfan TahirKheli

Reputation: 3662

Did you try using Validation Group property?

<asp:GridView ID="grvLocationCash" runat="server" AutoGenerateColumns="false" CssClass="gridtable" DataKeyNames="LocationId">
    <Columns>
        <asp:BoundField HeaderText="Location Name" DataField="LocationName" />
        <asp:TemplateField HeaderText="Amount">
            <ItemTemplate>
                <asp:TextBox ID="txtAmount" runat="server" CssClass="txtbox" Visible="true"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvAmount" runat="server" ControlToValidate="txtAmount" ErrorMessage="Please enter the Amount" Display="Dynamic" SetFocusOnError="true" ValidationGroup="save"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="rgvAmount" runat="server" ControlToValidate="txtAmount" ValidationExpression="^\d+$" ErrorMessage="Please enter whole numbers only" Display="Dynamic" SetFocusOnError="true" ValidationGroup="save"></asp:RegularExpressionValidator>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>


<asp:Button ID="btnLocationAmountList" runat="server" OnClick="btnLocationAmountList_Click" Text="Save" Width ="100px" ValidationGroup="save" />

Upvotes: 0

Karl Anderson
Karl Anderson

Reputation: 34846

You need to handle the onkeydown in each text box in each ItemTemplate, like this:

onkeydown = "return (event.keyCode!=13);"

<asp:TemplateField HeaderText="Amount">
    <ItemTemplate>
        <asp:TextBox ID="txtAmount" runat="server" CssClass="txtbox" 
                     Visible="true"
                     onkeydown = "return (event.keyCode != 13);">
        </asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>

Note: The Enter key has a keyCode value of 13.

Upvotes: 1

Related Questions