using required field validators to validate inputs for a textbox on an asp.net page

Hi.
I have an asp.net page with a couple of text boxes. I want to be able to add a project name  and add a client name independantly. there is a text box to enter a client name and then a button to submit that text, currently i have a required field validator so that an empty box cant be submitted, this was the only submission textbox and button on the page but now i need to add another textbox and submit button to add a project name.
My problem now is that i cant enter a project name without having some text in the client name box and vice versa. How do i get round this issue?

Many thanks
asp page
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddProject.aspx.cs" Inherits="AddProject" %>
<%@ Register TagPrefix="Nav" TagName="Navigation" src="Navigation.ascx"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Add Project</title>
    <link rel="stylesheet" type="text/css" href="default.css" />
</head>
<body>
    <div class="globalContainer">
     <div class="paddingOne">
        <div class="header">
            <div class="oneColumnContainer">
                   <h1>Clients Projects Site</h1>
                   <h2>Add projects page</h2>
            </div>
            <div class="oneColumnContainer strapline"><p>&nbsp;</p>
            </div>
       </div> 
    <div class="mainContainer index">   
   
      <div class="left">
    <form id="form1" runat="server">
        <div class="oneColumnContainer">
            <h3>Select the client to add a project to from the list below</h3>
        </div>
        <div class="threeColumnContainer index">
            <div class="columna">
                <asp:Label ID="Label1" AssociatedControlID="client" Text="Client:" runat="server" />
                <asp:DropDownList ID="client" runat="server" DataValueField="">
                        <asp:ListItem Value="" Text="Please select..." Selected="True" />
                    </asp:DropDownList>
             </div>
             <div class="column2">
                 <p><a href="ContactList.aspx">Click to view this clients list of contacts</a></p>
              </div>
              <div class="column3"> 
                 <p><a href="AddClient.aspx">Click to add a Client </a></p>
                 </div>
        </div>
        <div class="oneColumnContainer">
         <div class="paddingTwo">
        
          <asp:RequiredFieldValidator ID="RequiredFieldValidatorproj" ControlToValidate="client" runat="server" ErrorMessage="Please select a client name" Display="Dynamic" />
          </div>
        </div>
        <div class="threeColumnContainer">
            <div class="column1">
                <asp:Label ID="Label2" AssociatedControlID="project" Text="Project:" runat="server" />
                <asp:TextBox ID="project" runat="server" />
            </div>
            <div class="column2">    
                <asp:RequiredFieldValidator ControlToValidate="project" runat="server" ErrorMessage="Please provide a project name" Display="Dynamic" />
                <asp:CustomValidator ControlToValidate="project" OnServerValidate="CheckProjectName" runat="server" ErrorMessage="Project name has already been used" Display="Dynamic" />
             </div>
              <div class="column3">  
                <asp:Button ID="add_project" OnClick="AddAProject" runat="server" Text="Add Project" />
              </div>
       </div>
       <div class="oneColumnContainer button">
        <p><a href="ViewProject.aspx">Click to view lists of projects</a></p>
        </div>
    <div class="threeColumnContainer">
        <div class="column1">
            <asp:Label AssociatedControlID="NewClient" Text="Enter new client:" runat="server" />
            <asp:TextBox ID="NewClient" runat="server" />
        </div>
        <div class="column2">
            <asp:RequiredFieldValidator ControlToValidate="NewClient" runat="server" ErrorMessage="Please provide a Client name" Display="Dynamic" />
            <asp:CustomValidator ControlToValidate="NewClient" OnServerValidate="CheckClient" runat="server" ErrorMessage="Client name has already been used" Display="Dynamic" />
        </div>
        <div class="column3">
            <asp:Button ID="add_client" OnClick="AddAClient" runat="server" Text="Add Client" />
        </div>
       </div>
       </form>
      </div>
        <Nav:Navigation ID="Navigation1" runat="server" />
       
      </div>
     </div>
   </div>
</body>
</html>

Open in new window

CharlieDevAsked:
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.

sm394Commented:

set CausesValidation="false"
in second button
0
CharlieDevAuthor Commented:
i get a runtime error of

System.Web.HttpException: Page.IsValid cannot be called before validation has taken place. It should be queried in the event handler for a control that has CausesValidation=True and initiated the postback, or after a call to Page.Validate.
0
sm394Commented:
did you add CausesValidation="false" in the button aspx
<asp:Button ID="add_client" OnClick="AddAClient" runat="server"  CausesValidation="false" Text="Add Client" />
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

CharlieDevAuthor Commented:
yes, it gave me that error posted above. Any more ideas? thanks
0
OBonioCommented:
I think a better solution would be to use VaidationGroup. Then you can validate both the project name and the client name.

http://msdn.microsoft.com/en-us/library/ms227424(VS.80).aspx

0
sm394Commented:
yes ValidationGroup is a better option
0
OBonioCommented:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddProject.aspx.cs" Inherits="AddProject" %>
<%@ Register TagPrefix="Nav" TagName="Navigation" src="Navigation.ascx"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Add Project</title>
    <link rel="stylesheet" type="text/css" href="default.css" />
</head>
<body>
    <div class="globalContainer">
     <div class="paddingOne">
        <div class="header">
            <div class="oneColumnContainer">
                   <h1>Clients Projects Site</h1>
                   <h2>Add projects page</h2>
            </div>
            <div class="oneColumnContainer strapline"><p> </p>
            </div>
       </div>
    <div class="mainContainer index">  
   
      <div class="left">
    <form id="form1" runat="server">
        <div class="oneColumnContainer">
            <h3>Select the client to add a project to from the list below</h3>
        </div>
        <div class="threeColumnContainer index">
            <div class="columna">
                <asp:Label ID="Label1" AssociatedControlID="client" Text="Client:" runat="server" />
                <asp:DropDownList ID="client" runat="server" DataValueField="">
                        <asp:ListItem Value="" Text="Please select..." Selected="True" />
                    </asp:DropDownList>
             </div>
             <div class="column2">
                 <p><a href="ContactList.aspx">Click to view this clients list of contacts</a></p>
              </div>
              <div class="column3">
                 <p><a href="AddClient.aspx">Click to add a Client </a></p>
                 </div>
        </div>
        <div class="oneColumnContainer">
         <div class="paddingTwo">
       
          <asp:RequiredFieldValidator ID="RequiredFieldValidatorproj" ControlToValidate="client" runat="server" ErrorMessage="Please select a client name" Display="Dynamic" />
          </div>
        </div>
        <div class="threeColumnContainer">
            <div class="column1">
                <asp:Label ID="Label2" AssociatedControlID="project" Text="Project:" runat="server" />
                <asp:TextBox ID="project" runat="server" />
            </div>
            <div class="column2">    
                <asp:RequiredFieldValidator ControlToValidate="project" ValidationGroup="ProjectValidation" runat="server" ErrorMessage="Please provide a project name" Display="Dynamic" />
                <asp:CustomValidator ControlToValidate="project" OnServerValidate="CheckProjectName" ValidationGroup="ProjectValidation" runat="server" ErrorMessage="Project name has already been used" Display="Dynamic" />
             </div>
              <div class="column3">  
                <asp:Button ID="add_project" OnClick="AddAProject" runat="server" Text="Add Project" ValidationGroup="ProjectValidation" />
              </div>
       </div>
       <div class="oneColumnContainer button">
        <p><a href="ViewProject.aspx">Click to view lists of projects</a></p>
        </div>
    <div class="threeColumnContainer">
        <div class="column1">
            <asp:Label AssociatedControlID="NewClient" Text="Enter new client:" runat="server" />
            <asp:TextBox ID="NewClient" runat="server" />
        </div>
        <div class="column2">
            <asp:RequiredFieldValidator ControlToValidate="NewClient" ValidationGroup="ClientValidation" runat="server" ErrorMessage="Please provide a Client name" Display="Dynamic" />
            <asp:CustomValidator ControlToValidate="NewClient" OnServerValidate="CheckClient" ValidationGroup="ClientValidation" runat="server" ErrorMessage="Client name has already been used" Display="Dynamic" />
        </div>
        <div class="column3">
            <asp:Button ID="add_client" OnClick="AddAClient" runat="server" Text="Add Client" ValidationGroup="ClientValidation" />
        </div>
       </div>
       </form>
      </div>
        <Nav:Navigation ID="Navigation1" runat="server" />
       
      </div>
     </div>
   </div>
</body>
</html>
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
CharlieDevAuthor Commented:
Thanks for laying it out all clearly in code for me :)
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
.NET Programming

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.