locked
DefaultButton and the ValidationSummary Control RRS feed

  • Question

  • User1032812968 posted

    Scenario:

    I have an asp:panel control with the defaultbutton attribute set to a button within the panel.  The button is part of a validation group that includes a requiredvalidator control associated with a textbox (within the panel).  I also have a validationsummary control that is part of the validationgroup and also has its ShowMessageBox attribute set to true.  The validation summary displays correctly when I click on the button, but when I hit enter on the keyboard I get the text value of the required validator to show up but the validationsummary popup message does not appear. I posted the code below.  Any clues?

     

    <asp:UpdatePanel ID="upCustomerEdit" runat="server">
        <ContentTemplate>       
            <asp:Panel ID="panCustomerEdit" runat="server" DefaultButton="btnSave">
                        <table>
                                <tr>
                                    <td>Some label:</td>
                                    <td>
                                        <asp:Textbox ID="Textbox1" runat="server"></asp:DropDownList>
                                        <asp:RequiredFieldValidator ID="valTextbox1" runat="server" ControlToValidate="Textbox1" ValidationGroup="valGroupSave" EnableClientScript="true" Text="*" Display="Static" ErrorMessage="Textbox 1 value is required" SetFocusOnError="true"></asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <asp:Button ID="btnSave" runat="Server" Text="Save" ValidationGroup="valGroupSave" CausesValidation="true" />
                        </td>
                    </tr>
                </table>
            </asp:Panel>
            <asp:ValidationSummary ID="valSummary" runat="server"
                DisplayMode="BulletList"
                ShowMessageBox="True"
                ShowSummary="False"
                ValidationGroup="valGroupSave"
                HeaderText="Errors on Page:" />
        </ContentTemplate>
    </asp:UpdatePanel>

    Wednesday, April 18, 2007 4:52 PM

All replies

  • User-1690838931 posted

    The exact same problem plagues me also. The ValidationSummary does not show up when the enter key is being pressed.

    Has anyone yet found a solution for this one? [:(] 

     

    sincerely stawrogin 

    Tuesday, August 21, 2007 8:06 AM
  • User923504599 posted

    See if this post helps you out at all: http://forums.asp.net/p/985791/1798911.aspx

    -Damien

    Tuesday, August 21, 2007 8:20 AM
  • User-1690838931 posted

    Thanks for your suggestions. It is not about the UpdatePanel. In fact i don't use an UpdatePanel. It's about the ValidationSummary not being shown.

    Take this simple scenario (code below): A Loginform with username, password, and a Login-Button. The defaultFocus is on the username, the defaultButton is set to the Login-button. Additionally there are two RequiredFieldValidators and a ValidationSummary. When clicking the Login-Button all controls get evaluated and the ValidationSummary is being shown. When hitting enter, only the Text of the RequiredFieldValidator is shown but not the ValidationSummary. I want the Validation to be the same whether anyone clicks the button or hits enter. But how?


     <form id="form1" runat="server" defaultfocus="txtUserName" DefaultButton="btnLogin">
            <div>
                <div>
                    <h2>Login</h2>
                </div>
                <div>
                    <asp:Label ID=lblUserName runat=server AssociatedControlID="txtUserName" CssClass="w-117">Benutzername</asp:Label><asp:RequiredFieldValidator ID=reqUser runat=server Text=" *" ControlToValidate="txtUserName" ErrorMessage="Erforderliche Eingabe Benutzername." SetFocusOnError=true></asp:RequiredFieldValidator>
                    <asp:TextBox id="txtUserName" runat=server CausesValidation=true CssClass="w-153" />
                </div>
                <div>
                    <asp:Label ID=lblPassword runat=server AssociatedControlID="txtPassword" CssClass="w-117">Passwort</asp:Label><asp:RequiredFieldValidator ID=requPassword runat=server Text=" *" ControlToValidate="txtPassword" ErrorMessage="Erforderliche Eingabe Passwort." SetFocusOnError=true></asp:RequiredFieldValidator>
                    <asp:TextBox id="txtPassword" runat=server CausesValidation=true CssClass="w-153" TextMode=Password />
                    <asp:Button ID=btnLogin runat=server Text="Login" />
                </div>
                <asp:ValidationSummary ID=valSummaryLogin runat=server DisplayMode=BulletList ShowSummary=true/>
            </div>
        </form>

    Wednesday, August 29, 2007 7:09 AM
  • User1246221937 posted

    I have a similar issue. I have a Validation Summary out side of a GridView that has a Range Validator inside of it. The Range Validator will fire correctly with either pressing "Enter" or clicking the "Update" button (set as DefaultButton). However the Validation Summary will not update when I press "Enter", only when I click on the "Update" button.

     Anyone else have an idea?

    Wednesday, December 12, 2007 9:30 AM
  • User2021261141 posted

    After a more detailed study of the problem revealed that the construction 
    <Asp: Panel ID = "Panel1" runat = "server" DefaultButton = "btnFind"> 
    becomes 
    <div id="ctl00_cphMain_Panel1" onkeypress="javascript:return WebForm_FireDefaultButton(event,'ctl00_cphMain_btnFind')"> 
    But unfortunately, JavaScript onkeypress event of input field overridden by validator that controlling this field. So when you enter the wrong information and pressing the Enter button it's triggered validator, which displays the text of the error and stops processing events. 
    So that would overcome this "evil" enough to use a different event. In this case, it will keydown.It will look like this: 

    pnlAsset.Attributes.Add ( "onkeydown", "javascript: return WebForm_FireDefaultButton (event, '" + btnFind.ClientID + "')");

    Friday, December 4, 2009 8:25 AM
  • User532034571 posted

    Hi Igon,

    Great, your suggestion is working properly. I would like to add some few things for this. Some of them may have problems with browser capability. In this case, it's better to use below method for "Enter key submit option" and then you can add this JavaScript to the div section.

     

    protected void Page_Load(object sender, EventArgs e)

        {

            if (!Page.IsPostBack)

            {           

                form1.Attributes.Add("onkeydown", "javascript: return WebForm_FireDefaultButton (event, '" + ButtonName.ClientID + "')");

                ClientScript.RegisterHiddenField("__EVENTTARGET", "ButtonName");

            }

        }

    // Then use below code.

     


    <div id="inputArea" onkeypress="javascript:return WebForm_FireDefaultButton(event,'ButtonName')">

    This has been solved the Validation Summary display issue on enter key submit


    Friday, January 14, 2011 9:33 AM