Q1. What’ is the sequence in which ASP.NET events are processed ?
Ans - Following is the sequence in which the events occur :-
√ Page_Init.
√ Page_Load.
√ Control events
√ Page_Unload event.
Page_init event only occurs when first time the page is started, but Page_Load occurs in subsequent request of the page.
Q2. In which event are the controls fully loaded ?
Ans - During Page_load event all controls get fully loaded. Controls are also accessed in Page_Init events but you will see that viewstate is not fully loaded during this event.
Q3. How can we identify that the Page is PostBack ?
Ans - Page object has a "IsPostBack" property which can be checked to know that is the page posted back or not.
Q4. Where is ViewState information stored ?
Ans - Viewstate is stored in a HTML Hidden Field on the client side.
Q5. What is "AutoPostBack" feature in ASP.NET ?
Ans - If we want the control to automatically postback in case of any event, we will need to check this attribute as true. Example on a Dropdown change we need to send the event immediately to the server side then set the "AutoPostBack" attribute to true.
Wednesday, May 14, 2008
Friday, May 2, 2008
Delegates and Events
Q 1. What is a delegate ?
Ans - Delegate is a class that can hold a reference to a method or a function. A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. Delegate class has a signature and it can only reference those methods whose signature is compliant with the class. The delegate object can be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Delegates are type-safe functions pointers or callbacks.
An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references. Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.
The signature of a single cast delegate is shown below:
delegate result-type delname ([parameters]);
where:
result-type : The result type, which matches the return type of the function.
delname : The delegate name.
parameters: The Parameters, that the function takes.
A very basic example:
using System;
// Declaration
public delegate void SimpleDelegate();
class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("called by delegate ...");
}
public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
// Invocation
simpleDelegate();
}
}
OUTPUT
called by delegate ...
Q 2. What are events ?
Ans - As compared to delegates events works with source and listener methodology. So listeners who are interested in receiving some events they subscribe to the source. Once this subscription is done the source raises events to its entire listener when needed. One source can have multiple listeners.
Declaring an event is directly tied to a delegate. A delegate object encapsulates a method so that it can be called anonymously. An event is a mechanism by which a client class can pass in delegates to methods that need to be invoked whenever "something happens". When it does, the delegate(s) given to it by its clients are invoked.
To declare an event in C#, use the following syntax:
public delegate void testDelegate(int a);
public event testDelegate MyEvent;
Once an event is declared, it must be associated with one or more event handlers before it can be raised. An event handler is nothing but a method that is called using a delegate. Use the += operator to associate an event with an instance of a delegate that already exists.
For example:
Myform.MyEvent += new testEvent(MyMethod);
An event handler may also be detached as follows:
MyForm.MyEvent -= new testEvent(MyMethod);
In C#, events may be raised by just calling them by name similar to method invocation, say MyEvent(10).
Whenever an event is defined for a class, the compiler generates three methods that are used to manage the underlying delegate:
add_ : this is a public method that calls the static Combine method of System.Delegate in order to add another method to its internal invocation list. This method is however not used explicitly. The same effect is achieved by using the += operator as specified before.
remove_ : this is also a public method that calls the static Remove method of System.Delegate in order to remove a receiver from the event's invocation list. This method is also not called directly. Its job is done by the "-=" operator.
raise_ : a protected method that calls the compiler generated Invoke method of the delegate, in order to call each method in the invocation list.
An event handler in C# is a delegate with a special signature, as
public delegate void MyEventHandler(object sender, MyEventArgs e);
The first parameter (sender) in the above declaration specifies the object that fired the event. The second parameter (e) of the above declaration holds data that can be used in the event handler. The class MyEventArgs is derived from the class EventArgs. EventArgs is the base class of more specialized classes, like MouseEventArgs, ListChangedEventArgs, etc. For GUI event, you can use objects of these specialized EventArgs classes without creating your own specialized EventArgs classes. However, for non GUI event, you need to create your own specialized EventArgs class to hold your data that you want to pass to the delegate object. You create your specialized EventArgs class by deriving from EventArgs class.
public class MyEventArgs : EventArgs{
public string m_myEventArgumentdata;
}
In case of event handler, the delegate object is referenced using the key word event as follows:
public event MyEventHandler MyEvent;
Ans - Delegate is a class that can hold a reference to a method or a function. A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. Delegate class has a signature and it can only reference those methods whose signature is compliant with the class. The delegate object can be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Delegates are type-safe functions pointers or callbacks.
An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references. Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.
The signature of a single cast delegate is shown below:
delegate result-type delname ([parameters]);
where:
result-type : The result type, which matches the return type of the function.
delname : The delegate name.
parameters: The Parameters, that the function takes.
A very basic example:
using System;
// Declaration
public delegate void SimpleDelegate();
class TestDelegate
{
public static void MyFunc()
{
Console.WriteLine("called by delegate ...");
}
public static void Main()
{
// Instantiation
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
// Invocation
simpleDelegate();
}
}
OUTPUT
called by delegate ...
Q 2. What are events ?
Ans - As compared to delegates events works with source and listener methodology. So listeners who are interested in receiving some events they subscribe to the source. Once this subscription is done the source raises events to its entire listener when needed. One source can have multiple listeners.
Declaring an event is directly tied to a delegate. A delegate object encapsulates a method so that it can be called anonymously. An event is a mechanism by which a client class can pass in delegates to methods that need to be invoked whenever "something happens". When it does, the delegate(s) given to it by its clients are invoked.
To declare an event in C#, use the following syntax:
public delegate void testDelegate(int a);
public event testDelegate MyEvent;
Once an event is declared, it must be associated with one or more event handlers before it can be raised. An event handler is nothing but a method that is called using a delegate. Use the += operator to associate an event with an instance of a delegate that already exists.
For example:
Myform.MyEvent += new testEvent(MyMethod);
An event handler may also be detached as follows:
MyForm.MyEvent -= new testEvent(MyMethod);
In C#, events may be raised by just calling them by name similar to method invocation, say MyEvent(10).
Whenever an event is defined for a class, the compiler generates three methods that are used to manage the underlying delegate:
add_
remove_
raise_
An event handler in C# is a delegate with a special signature, as
public delegate void MyEventHandler(object sender, MyEventArgs e);
The first parameter (sender) in the above declaration specifies the object that fired the event. The second parameter (e) of the above declaration holds data that can be used in the event handler. The class MyEventArgs is derived from the class EventArgs. EventArgs is the base class of more specialized classes, like MouseEventArgs, ListChangedEventArgs, etc. For GUI event, you can use objects of these specialized EventArgs classes without creating your own specialized EventArgs classes. However, for non GUI event, you need to create your own specialized EventArgs class to hold your data that you want to pass to the delegate object. You create your specialized EventArgs class by deriving from EventArgs class.
public class MyEventArgs : EventArgs{
public string m_myEventArgumentdata;
}
In case of event handler, the delegate object is referenced using the key word event as follows:
public event MyEventHandler MyEvent;
Subscribe to:
Posts (Atom)