requiredfiledvalidator of one button affects a second button

metropia
metropia used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Owner, Lead Technology Programmer
Commented:
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.)
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
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.

Author

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

HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Bill NolanOwner, Lead Technology Programmer

Commented:
You don't need a button event handler if it does what you need already.
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

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.

Author

Commented:
Thank you greatly guys!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial