requiredfiledvalidator of one button affects a second button

I created a RequiredFieldValidator control, to ensure that a comment is enter before saving it.

the validator is control by a button named btnSaveLog

I created another button to redirect to another page, this button is independent from whether the suer has entered or not a comment on the textbox that has the validator assigned to.

For some reason, when I click the button to go to the other page, the error message of the validator for the text box gets triggered.

Can someone show me what am I doing wrong?

<tr>
            <td colspan="2" class="style6" valign="top">
                <asp:TextBox ID="txtComments" runat="server" Height="110px" TextMode="MultiLine" 
                    Width="410px" BorderColor="#CCCCFF" BorderStyle="Solid" BorderWidth="1px" 
                    Font-Names="Verdana" Font-Size="Small" CssClass="style5"></asp:TextBox><br />
                <asp:RequiredFieldValidator 
                    ID="reqComment" 
                    runat="server" 
                    ErrorMessage="YOU MUST ENTER A COMMENT BEFORE ADDING A NEW COMMENT." 
                    ControlToValidate="txtComments" 
                    InitialValue="" 
                    Font-Names="Verdana" 
                    Font-Size="X-Small" 
                    ForeColor="#FF0066" 
                    SetFocusOnError="False"
                    EnableClientScript="true">
                </asp:RequiredFieldValidator>
            </td>            
            <td colspan="4" class="style10" valign="top" align="right">
                <asp:TextBox ID="txtLastContactDateAndLog" runat="server" CssClass="style11" 
                    Height="107px" TextMode="MultiLine" Width="410px" BorderColor="#CCCCFF" 
                    BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="Small" 
                    ReadOnly="True"></asp:TextBox>
            </td>               
        </tr>        
        <tr>
            <td colspan="3">
                <asp:Button ID="btnSaveLog" runat="server" Text="Add New Comment" 
                    BorderStyle="Outset" Font-Names="Verdana" Font-Size="Small" 
                    onclick="btnSaveLog_Click" TabIndex="1"  Width="150" />
            </td>
            <td colspan="3" align="right">
                <asp:Button ID="btnViewHistory" runat="server" Text="View Log History" 
                    BorderStyle="Outset" Font-Names="Verdana" Font-Size="Small" TabIndex="2" 
                    CssClass="style22" Width="150" onclick="btnViewHistory_Click" />
            </td>
               
        </tr>

Open in new window

metropiaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill NolanOwner, Lead Technology ProgrammerCommented:
Two things to look into...

Button:
CausesValidation property (Probably will solve your problem.  However, be sure that you truly do not want validation anywhere on page.)

TextBox:
ValidationGroup (Read about exactly what this does.  You generally want to use this property with all validators.)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
käµfm³d 👽Commented:
This is because both buttons post back to the same page. This causes the logic for the RequiredFieldValidator to be executed in both instances. In addition to Slimfinger's suggestions, if all your second button does is redirect to the new page, you might set the PostBackUrl property to point to the new page. Then you are performing a post back to a different page, and your RequiredFieldValidator won't fire for that particular button.
0
metropiaAuthor Commented:
Hi guys,

I changed my page code after doing the readings that Slimfinger recommended, and also I added the property PostBackUrl to the button that redirects to another asp.net page.

question about the latest, if I use the property PostBackUrl  and enter the page url, do
I still need to add and event for the button click that redirects to that page?


I am including my code after the changes so that you can give me your thoughts.

Thanks a lot.



<td colspan="2" class="style6" valign="top">
    <asp:TextBox 
        ID="txtComments" 
        runat="server" 
        TextMode="MultiLine"></asp:TextBox><br />
    <asp:RequiredFieldValidator 
        ID="reqComment" 
        runat="server" 
        ErrorMessage="YOU MUST ENTER A COMMENT BEFORE ADDING A NEW COMMENT." 
        ControlToValidate="txtComments"
        ValidationGroup="AddNewComment" 
        SetFocusOnError="true"
        EnableClientScript="true">
    </asp:RequiredFieldValidator>
</td>

<td colspan="4" class="style10" valign="top" align="right">
	<asp:TextBox 
            ID="txtLastContactDateAndLog" 
            runat="server" 
            ReadOnly="True">
        </asp:TextBox>
</td>               

<td colspan="3">
	<asp:Button 
	    ID="btnSaveLog" 
	    runat="server" 
	    Text="Add New Comment" 
	    onclick="btnSaveLog_Click" 
	    CausesValidation="true" 
	    ValidationGroup="AddNewComment" />
</td>
<td colspan="3" align="right">
        <asp:Button 
            ID="btnViewHistory" 
            runat="server" 
	    Text="View Log History" 
	    onclick="btnViewHistory_Click" 
	    PostBackUrl="~/AR_Log_History.aspx" />
</td>

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Bill NolanOwner, Lead Technology ProgrammerCommented:
You don't need a button event handler if it does what you need already.
0
käµfm³d 👽Commented:
if I use the property PostBackUrl  and enter the page url, do
I still need to add and event for the button click that redirects to that page?
No.
0
metropiaAuthor Commented:
Thank you greatly guys!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.