Articles → .NET → Displaying Validation Error Message In Login Control In Asp.Net

Displaying Validation Error Message In Login Control In Asp.Net






Software Requirement





Prerequisite Knowledge




  1. Basics about .net programming
  2. How to create website using visual studio?
  3. Basics about validation controls in .net
  4. Basics about controls like login control, change password control etc.

Steps Of Execution




  1. Create a new project
  2. Add login control to the page
  3. Add validation Summary to the page
  4. Add code to bind validation group

Create A New Project




Picture showing the project structure in the solution explorer of the visual studio

Click to Enlarge


Add Login Control To The Page




<asp:Login ID="Login1" runat="server">
</asp:Login>



Add Validation Summary To The Page




<asp:Login ID="Login1" runat="server"></asp:Login>
<asp:ValidationSummary ID="summary" runat="server"/>



Add Code To Bind Validation Group




protected void Page_Load(object sender, EventArgs e) {
  summary.ValidationGroup = ((BaseValidator) Login1.FindControl("UserNameRequired")).ValidationGroup;
}



Code Explanation




<table cellspacing="0" cellpadding="1" id="MainContent_Login1" style="border-collapse:collapse;">
	<tr>
		<td>
			<table cellpadding="0">
				<tr>
					<td align="center" colspan="2">Log In</td>
				</tr>
				<tr>
					<td align="right">
						<label for="MainContent_Login1_UserName">User Name:</label>
					</td>
					<td>
						<input name="ctl00$MainContent$Login1$UserName" type="text" id="MainContent_Login1_UserName" />
						<span id="MainContent_Login1_UserNameRequired" title="User Name is required." style="visibility:hidden;">*</span>
					</td>
				</tr>
				<tr>
					<td align="right">
						<label for="MainContent_Login1_Password">Password:</label>
					</td>
					<td>
						<input name="ctl00$MainContent$Login1$Password" type="password" id="MainContent_Login1_Password" />
						<span id="MainContent_Login1_PasswordRequired" title="Password is required." style="visibility:hidden;">*</span>
					</td>
				</tr>
				<tr>
					<td colspan="2">
						<input id="MainContent_Login1_RememberMe" type="checkbox" name="ctl00$MainContent$Login1$RememberMe" />
						<label for="MainContent_Login1_RememberMe">Remember me next time.</label>
					</td>
				</tr>
				<tr>
					<td align="right" colspan="2">
						<input type="submit" name="ctl00$MainContent$Login1$LoginButton" value="Log In" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$Login1$LoginButton", "", true, "ctl00$MainContent$Login1", "", false, false))" id="MainContent_Login1_LoginButton" />
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>





Output


Picture showing the validation message applied on the login control in asp.net

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, February 21, 2013

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250