?
Solved

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

Posted on 2008-11-05
8
Medium Priority
?
1,208 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:CharlieDev
  • 3
  • 3
  • 2
8 Comments
 
LVL 13

Expert Comment

by:sm394
ID: 22884768

set CausesValidation="false"
in second button
0
 

Author Comment

by:CharlieDev
ID: 22884872
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
 
LVL 13

Expert Comment

by:sm394
ID: 22884893
did you add CausesValidation="false" in the button aspx
<asp:Button ID="add_client" OnClick="AddAClient" runat="server"  CausesValidation="false" Text="Add Client" />
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:CharlieDev
ID: 22884974
yes, it gave me that error posted above. Any more ideas? thanks
0
 
LVL 8

Expert Comment

by:OBonio
ID: 22884983
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
 
LVL 13

Expert Comment

by:sm394
ID: 22884998
yes ValidationGroup is a better option
0
 
LVL 8

Accepted Solution

by:
OBonio earned 2000 total points
ID: 22885011
<%@ 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
 

Author Closing Comment

by:CharlieDev
ID: 31513464
Thanks for laying it out all clearly in code for me :)
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question