Showing posts with label Custom Controls. Show all posts
Showing posts with label Custom Controls. Show all posts

Customizing VS.NET to Create a Run Method

You can customize Visual Studio .NET to create the Run() method for you each time you create a console application. Before doing this, make sure you have a current backup of your entire installation. Then follow these steps to create a template for console applications for use with this book:

  1. Navigate to the Templates directory. Typically this path is Program Files\ Microsoft Visual Studio .NET\ VC#\ VC#Wizards\ CSharpConsoleWiz\ Templates\ 1033.

  2. Make a copy of file1.cs.

  3. Open file1.cs to edit using Notepad or similar text editor.

  4. Remove whatever comments you no longer want in your console applications.

  5. Change the class name from [! output SAFE_CLASS NAME] to Tester.

  6. Remove the parameters to Main() and the attribute above it.

  7. Add the Run() method.

  8. Instantiate Tester and invoke Run from within Main().

  1. Save the file and start up Visual Studio .NET. Create a new console application to test that your code worked.

If at any time you decide you'd like to revert to the original template, simply restore the backup copy of file1.cs that you made earlier.

8.7.3 Using Static Fields

A common use of static member variables, or fields, is to keep track of the number of instances/objects that currently exist for your class. In the next example, you create a Cat class. The Cat class might be used in a pet-store simulation.

related post
DAY 1 MICROSOFT DOT NET FRAME WORK

DAY 2 MICROSOFT DOT NET BASE CLASS LIBRARY

DAY 3 MICROSOFT DOT NET CLASSES AND STRECTURES

DAY 4 METHODS IN FRAME WORK

DAY 5 INPUT VALIDATIONS IN DOT NET PART ONE

DAY 6 INPUT VALIDATIONS IN DOT NET PART TWO

DAY 7 DATA TYPES IN DOT NET

DAY 8 DATA TYPES IN DOT NET PART TWO

DAY 9 IMPLEMENTING PROPERTIES IN DOT NET

DAY 10 DELEGATES AND EVENTS

DAY 11 OOPS INTRODUCTION

DAY 12 POLYMORPHISM

DAY 13 INHERITANCE AND POLYMORPHISM

DAY 14 EBUGGING TOOLS IN DOT NET

DAY 15 DEBUG AND TRACE IN CLASSES

DAY 16 UNIT TEST PLAN

DAY 17 EXCEPTIONS IN VISUAL STUDIO

DAY 19 ADO.NET INTRODUCTION

DAY 20 DATA ACCESSING IN DOT NET

DAY 21 DATA BASE OBJECTS




Custom Controls in ASP.NET

Visual Studio .NET provides a rich set of standard controls, which are also called intrinsic controls. These standard controls provide a wide range of functionality and allow you to design and develop a user-friendly interface for your applications. Additionally, Visual Studio .NET provides you custom controls that you can use if the existing controls do not meet your requirements.

For example, consider a Web application that needs to have multiple Web Forms and most of the Web Forms need a calculator. In this case, instead of adding standard controls to each Web Form for implementing the calculator, you can create one custom control to represent a calculator and use it across the Web Forms in your application. Thus, custom controls allow reusability.

You can create the following types of custom controls in Visual Studio .NET:

User control:

A Web Forms page that can be used as a control on other Web Forms pages. Thus, if you already have a Web Forms page and you need to construct a similar one, you can use the existing page as a user control.

Composite control:

A combination of existing controls that is used as a single control. You can create a composite control in any .NET programming language and use it on an ASP.NET page. For example, you can create a composite control comprising a button and a text box in C# and use it on an ASP.NET page.

In addition to the custom controls discussed, you can perform the following actions with controls:

Extend the functionality of the existing Web Form controls to meet your requirements. For example, consider a situation in which an existing Web Forms control meets almost all of your requirements, but you need some additional features in the control. In such a situation, you can add more features to your Web Form and customize the control. This can be done by inheriting from the control and overriding its properties, methods, or events.

Develop a custom control by inheriting directly from one of the Control base classes. You'll need to do this when none of the existing Web Forms controls meets any of your requirements. The benefit of using the existing classes to create custom controls is that they provide the basic framework needed by a Web Forms control. This way, you can concentrate more on programming the features that you want to incorporate.

Before you create your own custom controls, let us examine the base classes used by the controls.

Basic Structure of Web Forms Controls

This section equips you with the basic understanding of the elements involved in developing a Web Forms control. We will first discuss the classes that are used to create Web Forms. This is followed by a discussion of the interfaces that can be implemented in Web Forms controls.

Classes used for Web Forms controls Each Web Forms control is a class that inherits from the System.Web.UI. Control class directly or indirectly. Therefore, in this section, we examine the System.Web.UI.Control class and its inherited classes that are used to create a Web Forms control.

The System.Web.UI.Control class

The System.Web.UI.Control class defines all the properties, events, and methods that are common to all the Web Forms controls. You need to inherit your control from this class in the following cases:

When your control does not have a user interface

When your control includes other controls that render their own user interface


The System.Web.UI.WebControls.WebControl class

The System.Web.UI.WebControls.WebControl class is the base class for all Web controls. This class provides properties and methods to implement user interface functionality. It is inherited from the Control class. Some of the properties that are used to render additional user interface functionality are ForeColor, BackColor, BorderStyle, Width, and Height. Web controls, such as Label, TextBox, Button, Table, and Calendar, all inherit from the WebControl class. Therefore, if you have a control that has a user interface, it should inherit from the WebControl class.

The System.Web.UI.HtmlControls.HtmlControl class

The HtmlControl class is the base class for all HTML controls in Web Forms. This class inherits from the Control class. The controls provided by this class map directly to HTML elements. Therefore, these controls are useful for migrating ASP applications to ASP.NET.

Interfaces used for Web Forms controls

Several interfaces are available that you can implement depending upon your requirements. For example, if your control provides data binding, you need to implement the INamingContainer interface. In this section, we examine the interfaces that you might need to implement when you create controls.

INamingContainer interface

This interface is used when you need to create controls that satisfy any of the following conditions:

Provides data binding
Is a templated control
Routes events to its child controls

The INamingContainer interface doesn't have methods. When this interface is implemented by a control, the ASP.NET page framework creates a namespace for the control and ensures that each child control in the parent control has a unique ID.

IPostBackDataHandler interface

A control should implement the System.Web.UI.IPostBackDataHandler interface when it needs to update its state or raise events on the server after examining the postback data.

For example, the data sent to a TextBox control might result in the text box changing its text, as determined by its Text property. When the text changes, the text box also raises a TextChanged event. Thus, this control is examining the data, changing its state, and raising events.

IPostBackEventHandler interface

The IPostBackEventHandler interface is implemented in a control when you want to transfer events that are generated on the client side to the server. Events generated on the client side are postback events, hence the interface name. An example of implementing this interface is when a user submits a form on the client side. In this case, the server does not know that the form is submitted unless the IPostBackEventHandler interface generates the Click event on the server when the form is submitted. This ensures that the server is in sync with the events that occur at the client end.

Developing a composite control

You can create new controls using one or more existing controls. Such controls that aggregate a number of controls are referred to as composite controls. The primary difference between composite controls and user controls is that composite controls are compiled into assemblies and the compiled file is included into the project in which you want to include the control. In this section, we will create a composite control in C# and use it on an ASP page.

Concepts involved in creating a composite control

When you create a composite control, you need to do the following:

Define the class that implements the composite control. The class needs to inherit from the Control class and, optionally, the INamingContainer class.

Optionally override the CreateChildControls method of the Control class. The CreateChildControls method creates any child controls that need to be drawn on a page. This method is used in compositionbased rendering, wherein child controls are instantiated and rendered on the page.

Optionally implement the Render method of the Control class. You need to implement this method when you use the rendering logic instead of composition to render the ASP page. When you render controls, the performance overhead is less because controls need not be instantiated. Instead, the page is rendered as defined by the Render method. The Render method controls the output of the page at run-time.

Creating the control

You have examined the basic concepts to create a control. Let us now create a composite control. We will create the control in C#. The same programming logic can be used in VB.NET as well, except that the syntax will change. The control that we will create comprises a Calendar control, a TextBox control, a Submit button, and a Label control. The user is expected to select his or her date of birth from the calendar, specify their work experience in years, and click the Submit button to ascertain whether he or she is eligible for a job.

To create the custom control project, create a new Class Library project in C#.

The controls that you need to use on the form are in the System.Web namespace.

Therefore, include a reference to the System.Web.dll file. To include the reference, you
need to perform the following steps:

1. Select the Add Reference option from the Project menu. The Add Reference dialog box appears.
2. In the Add Reference dialog box, from the .NET tab, select System.Web.dll and click Select.
3. The component moves to the Selected Components list. Click OK to add the reference.

After you add a reference to the System.Web.dll file, you can write the code for the control. To code the control, select the class module from the Solution Explorer. In the class module, declare the namespaces that are used by the control by specifying the following statements:
using System;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

Next, declare the namespace for the control and declare a public class in the namespace. You should also declare the properties that you need to expose for the control. In the following code, we have declared the namespace, class, and a Text property for an EmpElg label. Additionally, we have also left placeholders for the CreateChildControls method and the Click event of the Submit button.

RELATED POST

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO

IIS with Windows

Below are the benefits of using IIS 6.0 with Windows Server 2003.

1. Faster sites without the need for high spec hardware for Web sites, intranets, and Web-based applications.

2. You can check your site's potential speed improvement with real-time Compression Check tool provided by Port80 Software.

3. Reduces total response time per page request. Using HTTP compression helps end users experience a 50% reduction in download time.

4. HTTP compression reduces the size of files sent from your Web server to the Web browser by an average of 30 to 50 percent which is a dramatic decrease in bandwidth.

5. Using IIS 6.0 compression feature you can have savings in monthly bandwidth charges.

6. You can reduce bandwidth by enabling compression, and serve more users with the same hardware.

7. You can delay or eliminate the need to add more Web servers by enabling IIS 6.0 compression, even when your site gets more traffic.

8. Management code for compression is now part of core IIS 6.0 Web server for speed and stability.

9. Server-side compatibility issues are resolved.

10. Granular compression allows turning compression on/off at the global, site, folder, and file levels.

What is State Management and what are the different ways to maintain a state in .Net?

Web pages are recreated each time a page is posted to a server. In traditional Web programming, this would ordinarily mean that all information associated with the page and the controls within the page would be lost with each round trip.

To overcome this inherent limitation of traditional Web programming, the ASP.NET page framework includes various options to help you preserve changes when Managing the State. The page framework includes a facility called view state that automatically preserves property values of the page and all controls on it between round trips.
However, you will probably also have application-specific values that you want to preserve. This can be done using state management options.

Client-Based State Management Options: View State Hidden Form Fields Cookies Query Strings

Server-Based State Management Options: Application State Session State Database Support

What server controls come with ASP.NET?

Server controls are supplied with ASP.NET. Server controls in ASP.NET are different to the normal windows controls as they work within the .NET FrameWork. The different types of server controls in ASP.NET are:

• HTML Server Controls: These controls refer to the HTML elements that can be used in server code. The HTML elements can be converted into HTML server controls.

• ASP.NET Server controls: ASP.NET server controls are the general controls like textbox, buttons, tables etc.

• Validation Controls: these controls are used to validate user input. Normally these controls are attached with textboxes, checkboxes and radio buttons to check the values entered or selected.

• USER Controls: These controls are those controls that are created by the user and are used just like all other controls.

What is the AdRotator control and it's uses?

AdRotator is a special control in ASP.NET used to display flashing banner advertisments. The control can display advertisments randomly or sequentially as set by the developer.

Using the AdRotator control

To use the control, drag it from the toolbox. In the properties window you will there are three key properties:

.AdvertisementsFile .KeywordFilter .Target

The advertisement file is an XML file which contains information about the image to be displayed and the page to which the user should be redirected on click.

What is a Calendar control and how do I use it?

The Calendar control is used to display the calendar year on a web page. You can view dates or select a specific day, week or month. The default event of the Calendar control is Selection Changed event that is called when a selection is made on the control.

Drag a calendar control and a textbox from the toolbox. We will display the date selected from the calendar control in the textbox.

What is a RequiredFieldValidator control?

The RequiredFieldValidator is a control that checks whether the value of an assigned input control is different from its initial value.

To work with this control drag a RequiredFieldValidator control, Command Button and two TextBoxes from the Toolbox. The intention is to display the text entered in TextBox1 in TextBox2 when the Command Button is clicked. If there is no value in TextBox1, an error message ("Saying Textbox1 needs a value") will be spawned.

All you have to make the RequiredFieldValidator work is to open the properties of the control. Looking at two properties; ErrorMessage and ControltoValidate specifically. The ErrorMessage property should be completed with a warning that the field needs to be completed. Secondly, the ControltoValidate property is used to tag which control is to be validated.

RELATED POST

ASP.NET INTERVIEW QUESTIONS AND ANSWERS PART ONE

ASP.NET INTERVIEW QUESTIONS AND ANSWERS PART TWO

ASP.NET INTERVIEW QUESTIONS AND ANSWERS PART THREE

ASP.NET INTERVIEW QUESTIONS AND ANSWERS PART FOUR

ASP.NET INTERVIEW QUESTIONS AND ANSWERS PART FIVE

ADO.NET INTERVIEW QUESTIONS AND ANSWERS PART ONE

ADO.NET INTERVIEW QUESTIONS AND ANSWERS PART TWO

You can also learn the concept of frame work concept in detail with questions and answers in the following place.

MICROSOFT DOT NET FRAME WORK QUESTIONS AND ANSWERS PART ONE

MICROSOFT DOT NET FRAME WORK QUESTIONS AND ANSWERS PART TWO

MICROSOFT DOT NET FRAME WORK QUESTIONS AND ANSWERS PART THREE

MICROSOFT DOT NET FRAME WORK QUESTIONS AND ANSWERS PART FOUR

MICROSOFT DOT NET FRAME WORK QUESTIONS AND ANSWERS PART FIVE

MICROSOFT DOT NET FRAME WORK QUESTIONS AND ANSWERS PART SIX

MICROSOFT DOT NET FRAME WORK QUESTIONS AND ANSWERS PART SEVEN

MICROSOFT DOT NET FRAME WORK QUESTIONS AND ANSWERS PART EIGHT

MICROSOFT DOT NET FRAME WORK QUESTIONS AND ANSWERS PART NINE

MICROSOFT DOT NET FRAME WORK QUESTIONS AND ANSWERS PART TEN