Question 1) TS: Web-Based Client Development (VB.NET)

Posted on
Share on Google+Share on LinkedInShare on FacebookShare on RedditTweet about this on TwitterEmail this to someone

Objective: Creating and Programming a Web Application
SubObjective: Implement site navigation and input validation

Single Answer Multiple Choice

 

You are a Web application developer for your company. You are creating an ASP.NET application that will be used by the employees of the company to maintain their personal information.

 

Employees specify their employee ID and personal information, such as name, address, and bank account number on a registration Web page. Then, the employees click the submit Button Web server control to post the information to the server.

 

You add the following code to the registration Web page:

 

<asp:TextBox ID=”txtEmpId” runat=”server” />

 

<asp:RequiredFieldValidator ID=”RequiredFieldValidator1″ runat=”server”

 

ControlToValidate=”txtEmpId”

 

ErrorMessage=”Employee Id cannot be blank” />

 

<asp:CustomValidator ID=”CustomValidator1″ runat=”server”

 

ControlToValidate=”txtEmpId”

 

ErrorMessage=”EmployeeID is not valid” />

 

<asp:Button ID=”Submit” runat=”server” Text=”Button” />

 

To validate the employee ID specified by the employees against the employee ID values stored in a SQL Server database table named employee_master, you add the following function named ChkEmpId to the code-behind page of the registration Web page:

 

Function ChkEmpID(ByVal empid As String) As Boolean

 

‘Code if Employee Id value is valid value in the database

 

‘Code if Employee Id value is invalid

 

End Function

 

You want to ensure the registration Web page is not posted to the server if employee ID specified by employees is not valid.

 

Which code should you add?

 

A. Private Sub CustomValidator1_ServerValidate(ByVal _
source As System.Object, ByVal args As _
System.Web.UI.WebControls.ServerValidateEventArgs) _
Handles CustomValidator1.ServerValidate
If ChkEmpID(txtEmpId.Text) Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub

 

B. Private Sub RequiredFieldValidator1_ServerValidate(ByVal _
source As System.Object, ByVal args As _
System.Web.UI.WebControls.ServerValidateEventArgs) _
Handles RequiredValidator1.ServerValidate
If ChkEmpID(txtEmpId.Text) Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub

 

C. Private Function CustomValidator1_ServerValidate(ByVal _
source As System.Object, ByVal args As _
System.Web.UI.WebControls.ServerValidateEventArgs) _
Handles CustomValidator1.ServerValidate
If ChkEmpID(txtEmpId.Text) Then
Return True
Else
Return False
End If
End Function

 

D. Private Function CustomValidator1_ServerValidate(ByVal _
source As System.Object, ByVal args As _
System.Web.UI.WebControls.ServerValidateEventArgs) _
Handles RequiredValidator1.ServerValidate
If ChkEmpID(txtEmpId.Text) Then
Return True
Else
Return False
End If
End Function

 

Answer:
A. Private Sub CustomValidator1_ServerValidate(ByVal _
source As System.Object, ByVal args As _
System.Web.UI.WebControls.ServerValidateEventArgs) _
Handles CustomValidator1.ServerValidate
If ChkEmpID(txtEmpId.Text) Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub

 

Tutorial:
You should use the CustomValidator1_ServerValidate event handler. Web server validation controls, such as RequiredFieldValidator, RangeValidator, CompareValidator, and RegularExpressionValidator are typically used to validate user input in the Web server controls. The CustomValidator Web server control is used when you want to validate user inputs by using a custom function. In this scenario, you are required to validate user inputs against SQL Server database values. This validation cannot be performed by Validation Web server controls, such as RequiredFieldValidator, RangeValidator, CompareValidator, and RegularExpressionValidator. Therefore, you are required to use the CustomValidator Web server control that raises the ServerValidate event when validation is performed on the Web server. The ServerValidate event handler provides the args.IsValid property that is used to specify whether the value of the Web server control specified in the ControlToValidate property of the CustomValidator Web server control is valid or not. This code sets the args.IsValid property to true if ChkEmpId function returns true and allows posting of the Web page to the server.

 

You should not use the codes that are using RequiredFieldValidator1_ServerValidate event handler because this is not a valid event handler. The RequiredFieldValidator Web server control does not support the ServerValidate event. If used, the RequiredFieldValidator1_ServerValidate event handler will generate a run-time error.

 

You should not use the codes that return a true value because eventhandlers do not return values and must be declared as a Subroutine, not a Function.

 

Reference:
MSDN2 Library, Search, “Development Tools and Languages,” “Visual Studio,” “Visual Web Developer,” “Web Server Controls,” “Individual Web and HTML Controls,” “Validation Controls,” “Getting Started,” “Types of Validation for ASP.NET Server Controls,” “How to: Validate Against Values in a Database for ASP.NET Server Controls.”

 

These questions are derived from the Self Test Software Practice Test for Microsoft exam #70-528 – TS: Microsoft .NET Framework 2.0 – Web-Based Client Development (VB.NET)

Share on Google+Share on LinkedInShare on FacebookShare on RedditTweet about this on TwitterEmail this to someone
cmadmin

ABOUT THE AUTHOR

Posted in Archive|

Comment:

Leave a comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>