2
votes

Thank you very much for taking the time to read this. I have an issue in which I was setting a default button for an ASP.NET page on page load from code behind, but now that I have multiple validation groups targeting one control, that is no longer working. Now, when I hit enter while in that control (textbox), validation for both groups are triggered minus the validation summary text.

Here is my examplefied code:

ASPX

<table>
    <tr>
        <td><asp:Textbox runat="server" ID="validateMe"></asp:TextBox></td>
        <td><asp:RequiredFieldValidator ID="firstValidator" runat="server" ErrorMessage="First check not valid" Text="*" ControlToValidate="validateMe" ValidationGroup="firstGroup"></asp:RequiredFieldValidator>
        <td><asp:RequiredFieldValidator ID="secondValidator" runat="server" ErrorMessage="Second check not valid" Text="*" ControlToValidate="validateMe" ValidationGroup="secondGroup"></asp:RequiredFieldValidator>
    </tr>
    <tr>
    <td><asp:Button runat="server" ID="firstButton" Text="V1" ValidationGroup="firstGroup"/></td>
    <td><asp:Button runat="server" ID="secondButton" Text="V2" ValidationGroup="secondGroup"/></td>
    </tr>
<table>

<asp:ValidationSummary ID="firstSummary" runat="server" ValidationGroup="firstGroup"/>
<asp:ValidationSummary ID="secondSummary" runat="server" ValidationGroup="secondGroup"/>

C#

protected void Page_Load(object sender, EventArgs e)
{
    this.Form.DefaultButton = firstButton.UniqueID;
}

If I use this and hit 'Enter' while inside of the textbox without typing anything into it, then neither of the validation summaries will appear but I will have two asterisks next to the textbox (one for each group). If the user presses 'Enter' I would expect a full validation using only the first group which is supposed to be assigned to the DefaultButton (here, 'firstButton'). Is there any way to achieve this functionality and initiate the client-side validation that would have happened had the user clicked 'firstButton' instead?

I have also tried wrapping the whole table plus the validation summaries inside of an asp:Panel and setting the DefaultButton there, but I received the same results. Any help or pointers in the right direction would be greatly appreciated!

1
Try taking a look at the examples on here: w3schools.com/aspnet/control_validationsummary.asp They are good solid examples and will explain it better then i ever can. - BAS
My apologies DMA, but I haven't found anything on the w3 site that can help with this issue yet. The validation summary and default button pages contain very simple explanations of these controls and don't offer any solutions I have not already tried, nor any explanations as to why the default button would not be working when multiple validations are present on a single control. - user2912928

1 Answers

1
votes

Set

EnableClientScript="false"

in RequiredFieldValidator control. It will help.

<asp:Panel runat="server" DefaultButton="secondButton">
    <table>
        <tr>
            <td>
                <asp:TextBox runat="server" ID="validateMe"></asp:TextBox></td>
            <td>
                <asp:RequiredFieldValidator ID="firstValidator" runat="server" ErrorMessage="First check not valid" Text="*" ControlToValidate="validateMe" EnableClientScript="false" ValidationGroup="firstGroup"></asp:RequiredFieldValidator>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="secondValidator" runat="server" ErrorMessage="Second check not valid" Text="*" ControlToValidate="validateMe" EnableClientScript="false" ValidationGroup="secondGroup"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button runat="server" ID="firstButton" Text="V1" ValidationGroup="firstGroup" /></td>
            <td>
                <asp:Button runat="server" ID="secondButton" Text="V2" ValidationGroup="secondGroup" /></td>
        </tr>
    </table>

    <asp:ValidationSummary ID="firstSummary" runat="server" ValidationGroup="firstGroup" />
    <asp:ValidationSummary ID="secondSummary" runat="server" ValidationGroup="secondGroup" />
</asp:Panel>