1
votes

I'm using two contact forms in the same aspx page, one has 4 textbox controls (name- email- subject- body) and send button control. The second form has 2 textbox controls and send button, all (except the second textbox of the second form) has validation controls (requiredFieldValidator).

This is the code inside the first button click event:

  protected void ButtonSend_Click(object sender, EventArgs e)
    {
        if(Page.IsValid)
        {
            LabelIRespond.Visible = true;
            .....
        }
    } 

there is no code in the second button click event. the problem is, when the first form validated successful and the second form not validated, the code inside click event not executed. and the error message of the second form appears. They act like they are one form!

The markup for both forms:

<section id="sendSection">   
    <div id="contactMe">
        <h1>Contact me</h1>
    </div>
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>

    <h4>Send me a message:</h4>
    <table class="auto-style1">
        <tr>
            <td class="auto-style8">
                <asp:TextBox ID="TextBoxEmail" placeholder="Your Email address" runat="server" BackColor="#D4D8DF"  Font-Names="Century Gothic" CssClass="txtbxs" Height="25px" Width="250px"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorEmail" runat="server" ControlToValidate="TextBoxEmail" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Please enter your Email address">*</asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBoxEmail" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="RegularExpressionValidator" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style8">
                <asp:TextBox ID="TextBoxName" placeholder="Your name" runat="server" BackColor="#D4D8DF" Font-Names="Century Gothic" CssClass="txtbxs" Height="25px" Width="250px"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ControlToValidate="TextBoxName" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style5">
                <asp:TextBox ID="TextBoxSubject" placeholder="Subject" runat="server" BackColor="#D4D8DF" Font-Names="Century Gothic" CssClass="txtbxs" Height="25px" Width="250px"></asp:TextBox>
            </td>
            <td class="auto-style3">
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorSubject" runat="server" ControlToValidate="TextBoxSubject" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style6">
                <asp:TextBox ID="TextBoxBody" placeholder="What do you want to tell me?"  runat="server" BackColor="#D4D8DF" TextMode="MultiLine" Font-Names="Century Gothic" CssClass="txtbxs" Height="180px" Width="370px"></asp:TextBox>
            </td>
            <td class="auto-style2">
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorBody" runat="server" ControlToValidate="TextBoxBody" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="Type your message">*</asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="auto-style8">
                <asp:Button ID="ButtonSend" runat="server" Text="Send" CssClass="btnSend" Width="80px" OnClick="ButtonSend_Click" UseSubmitBehavior="False" Font-Names="Century Gothic" />
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="auto-style8">
                <asp:Label ID="LabelIRespond" runat="server" Text="Thanks, your message has been sent. I'll respond ASAP." Visible="False"></asp:Label>
            </td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
                        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
        <ProgressTemplate>
            <div class="wait">
            </div>
        </ProgressTemplate>
    </asp:UpdateProgress>

</section>                 
<div class="split"></div>

<section id="viewCVSection">
    <div id="check">
        <h1>Check out my CV</h1>
    </div>



    <div class="cvForm">
<table class="auto-style1">
    <tr>
        <td class="auto-style4">
            <asp:TextBox ID="TextBoxEmail2" placeholder="Company Email address" CssClass="txtbxs" runat="server" BackColor="#D4D8DF" Height="25px" Width="250px" Font-Names="Century Gothic"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBoxEmail2" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="TextBoxEmail2" CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="RegularExpressionValidator" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
        </td>
    </tr>
    <tr>
        <td class="auto-style4">
            <asp:TextBox ID="TextBoxCompany" placeholder="Company name" CssClass="txtbxs" runat="server" BackColor="#D4D8DF" Height="25px" Width="250px" Font-Names="Century Gothic"></asp:TextBox>
        </td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td class="auto-style4">
            <asp:Button ID="Button1" CssClass="sendCV" runat="server" Text="Request CV" Font-Names="Century Gothic" OnClick="Button1_Click" />
        </td>
        <td>&nbsp;</td>
    </tr>
</table>

    </div>
    </section>
1
You need to use ValidationGroup as mentioned in my answer. Set ValidationGroup for first contact form button and its validators to FirstGroup and then set the same property for second contact form button and validators to SecondGroup. It will work then. - Sunil
Also ButtonSend1 is in which contact form? - Sunil
first form. I've edited my question so it contain the markup for both forms now - user4451265
Can you follow what I said in my answer? - Sunil

1 Answers

2
votes

You need to hook up validation controls with the correct button. Right now, button click in first form is causing validation in first contact form as well as second contact form, which is causing Page.IsValid to be false since second form fields are empty and therefore invalid.

So set the validation group of first contact form different from the second contact form as in code below. Then set the ValidationGroup on corresponding validation controls with the same value.

<asp:Button ID="ButtonSend" runat="server" Text="Send" CssClass="btnSend"
 Width="80px" OnClick="ButtonSend_Click" UseSubmitBehavior="False" 
   Font-Names="Century Gothic" ValidationGroup="FirstGroup"/>



<asp:Button ID="Button1" CssClass="sendCV" runat="server" 
 Text="Request CV" Font-Names="Century Gothic" OnClick="Button1_Click" 
  ValidationGroup="SecondGroup"/>

NOTE: Make sure that all validation controls have their ValidationGroup also set to appropriate value after you make above code change.