You are on page 1of 9

Q:5

Client-side state management


This maintains information on the client's machine using Cookies, View State, and Query Strings.
Cookies.
A cookie is a small text file on the client machine either in the client's file system or memory of
client browser session. Cookies are not good for sensitive data. Moreover, Cookies can be
disabled on the browser. Thus, you can't rely on cookies for state management.
Hidden Fields - Hidden Fields are similar to a text box but does not get displayed on the
UI. However you can see the value of hidden fields when you view page source in the
browser. Using Hidden Fields, you can pass information from one page to another page
without the end user's knowledge, primarily for internal processing.
View State
Each page and each control on the page has View State property. This property allows automatic
retention of page and controls state between each trip to server. This means control value is
maintained between page postbacks. Viewstate is implemented using _VIEWSTATE, a hidden
form field which gets created automatically on each page. You can't transmit data to other page
using view state.
Querystring
Querystring can maintain limited state information. Data can be passed from one page to another
with the URL but you can send limited size of data with the URL. Most browsers allow a limit of
255 characters on URL length.
Server-side state management
This kind of mechanism retains state in the server.
Application State
The data stored in an application object can be shared by all the sessions of the application. The
application object stores data in the key value pair.
Session State
Session state stores session-specific information and the information is visible within the session
only. ASP.NET creates unique sessionId for each session of the application. SessionIDs are
maintained either by an HTTP cookie or a modified URL, as set in the application's
configuration settings. By default, SessionID values are stored in a cookie.
Cache - The Cache object is an instance of the System.Web.Caching.Cache class. Cache is
stored on the server side and is more scalable in nature, as ASP.NET removes objects if the
memory becomes scarce. This also makes it unreliable in some cases. Cache objects can
have expiration polices set on them and is shared across users.
Profile - Profile data is stored in the SQL Server database by default. This database
structure is preset, so if you want any custom user details to be stored, you will need to
create a custom database and write a custom provider for it. Profiles are designed to store
information permanently.
Context.Items - The HttpContext object is provided by the Page.Context property. The
HttpContext.Items collection can be used to temporarily store data across postback. View
state and session state can be used for a similar effect, but they assume longer-term
storage. Context can be used for storing data for one request only.
Q.8
validation controls?

Validation is important part of any web application. User's input must always be validated before sending
across different layers of the application.

Validation controls are used to:
Implement presentation logic.
To validate user input data.
Data format, data type and data range is used for validation.
There are six types of validation controls in ASP.NET
1. RequiredFieldValidation Control
2. CompareValidator Control
3. RangeValidator Control
4. RegularExpressionValidator Control
5. CustomValidator Control
6. ValidationSummary
RequiredFieldValidation Control

The RequiredFieldValidator control is simple validation control, which checks to see if the data is entered
for the input control. You can have a RequiredFieldValidator control for each form element on which you
wish to enforce Mandatory Field rule.

<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" Style="top: 98px;
left: 367px; position: absolute; height: 26px; width: 162px" ErrorMessage="password required"
ControlToValidate="TextBox2"></asp:RequiredFieldValidator>

CompareValidator Control

The CompareValidator control allows you to make comparison to compare data entered in an input
control with a constant value or a value in a different control.

It can most commonly be used when you need to confirm password entered by the user at the
registration time. The data is always case sensitive.

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" Style="top: 145px;
left: 367px; position: absolute; height: 26px; width: 162px" ErrorMessage="password required"
ControlToValidate="TextBox3"></asp:RequiredFieldValidator>

RangeValidator Control

The RangeValidator Server Control is another validator control, which checks to see if a control value is
within a valid range. The attributes that are necessary to this control are: MaximumValue, MinimumValue,
and Type.

<asp:RangeValidator ID="RangeValidator1" runat="server" Style="top: 194px; left: 365px;
position: absolute; height: 22px; width: 105px"
ErrorMessage="RangeValidator" ControlToValidate="TextBox4" MaximumValue="100"
MinimumValue="18" Type="Integer"></asp:RangeValidator>
RegularExpressionValidator Control

A regular expression is a powerful pattern matching language that can be used to identify simple and
complex characters sequence that would otherwise require writing code to perform.

Using RegularExpressionValidator server control, you can check a user's input based on a pattern that you
define using a regular expression.
In the example I have checked the email id format:
<asp:RegularExpressionValidator ID="string"
runat="server"
ErrorMessage="string"
ValidationExpression="string"
ValidationGroup="string">
</asp:RegularExpressionValidator>

CustomValidator Control

You can solve your purpose with ASP.NET validation control. But if you still don't find solution you can
create your own custom validator control.

The CustomValidator Control can be used on client side and server side. JavaScript is used to do client
validation and you can use any .NET language to do server side validation.
<asp:CustomValidator ID="CustomValidator1"
runat="server"
ClientValidationFunction=.cvf_func.
ErrorMessage="CustomValidator">
</asp:CustomValidator>
ValidationSummary

ASP.NET has provided an additional control that complements the validator controls.

The ValidationSummary control is reporting control, which is used by the other validation controls on a
page.

You can use this validation control to consolidate errors reporting for all the validation errors that occur
on a page instead of leaving this up to each and every individual validation control.

The validation summary control will collect all the error messages of all the non-valid controls and put
them in a tidy list.

<asp:ValidationSummary ID="ValidationSummary1" runat="server"
style="top: 390px; left: 44px; position: absolute; height: 38px; width: 625px" />

Both ErrorMessage and Text properties are used to display error messages. Text error message have
precedence.

Q.2
Preprocessor directives are commands that are interpreted by the compiler and affect the output or
behavior of the build process. But the C# compiler does not have a separate preprocessor, like C and
C++ you cannot use these directives to create macros. Preprocessing directives are top lines in our
program that start with '#'. The '#' is followed by an identifier that is the directive name.
Classification of C# language preprocessor directives are as follows.
Conditional compilation: We can include and exclude parts of the program based on the
conditions.
#if
#else
#elif
#endif
#define
#undef
Errors , Warnings , Line & pragma: The directive #error initiates the preprocessor to rise error,
#warning like #error directive but it prompts warning to the user and continues with the process,
#line can be used to hide sections of code from the debugger. The #pragma directive can either
suppress or restore specific compiler warnings.
#warning
#error
#line
#pragma
Region: If you want to indicate a certain block of source code with a name, you can indicate it with a
name and keep the entire block between #region and #endregion. So the C# code file is neatly
organized as blocks, and that can be expanded or collapsed visually.
#region
#endregion

Q.7
ASP.Net server controls are the primary controls used in ASP.Net. These controls again could be grouped into the
following categories:
Validation controls - these are used to validate user input and work by running client-side script
Data source controls - these controls provides data binding to different data sources
Data view controls - these are various lists and tables, which can bind to data from data sources for display
Personalization controls - these are used for personalization of a page according to the user's preference,
based on user information
Rich controls - these implements special features, for example, AdRotator control, FileUpload control,
Calendar control etc.
The basic syntax for using server controls is:
<asp:controlType ID ="ControlID"
runat="server"
Property1=value1 [Property2=value2] />
However, visual studio has the following features, which helps in error free coding:
Dragging and dropping of controls in design view
IntelliSense feature that displays and auto-completes the properties
The properties window to set the property values directly
Example:
Let us look at a particular server control - a tree view control. A Tree view control comes under navigation
controls. Other Navigation controls are: Menu control and SiteMapPath control.
Add a tree view control on the page. Select Edit Nodes... from the tasks. Edit each of the nodes using the
Tree view node editor:

Once you have created the nodes, it looks like the following in design view:

The AutoFormat... task allows you to format the tree view:

Add a label control and a text box control on the page and name them lblmessage and txtmessage
respectively.
Write few lines of code to ensure that when a particular node is selected, the label control displays the node
text and the text box displays all child nodes under it, if any. The code behind file should look like this:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace eventdemo
{
public partial class treeviewdemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
txtmessage.Text = " ";
}
protected void TreeView1_SelectedNodeChanged(object sender,
EventArgs e)
{
txtmessage.Text = " ";
lblmessage.Text = "Selected node changed to: " +
TreeView1.SelectedNode.Text;
TreeNodeCollection childnodes =
TreeView1.SelectedNode.ChildNodes;
if(childnodes != null)
{
txtmessage.Text = " ";
foreach (TreeNode t in childnodes)
{
txtmessage.Text += t.Value;
}
}
}
}
}
Run the page to see the effects. You will be able to expand and control the nodes.

You might also like