Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Event bubbling in .net

What's a bubbled event?

When you have a complex control, like DataGrid, writing an event processing routine for each object (cell, button,row, etc.) is quite tedious. The controls can bubble up their eventhandlers, allowing the main DataGrid event handler to take care of its constituents.

In simple language this is a process of calling a parent control's event from a child control is called Event bubbling.

Event bubbling in Detail:

For every control in asp.net there are some pre-defined events for example OnClick() event of button whenever a single click is made to that button that event raises. But if that particular button is in any of the following Server controls like GridView, Repeater, DataList etc.

But if same button is used inside GridView, We cannot retrieve the button onClick() event directly.

Here Button is the child control whereas the parent control is our Gridview. These child controls do not raise by themselves when they are inside any server controls, they pass the event to the container parent and from then it is passed to our page as " RowCommand ". From the code behind you can access button onclick event now with that RowCommand  Event of Parent control.This process of sending the child control events to parent control is said to be Event Bubbling.

Example:

Event bubbling can be accomplished using row command event of grid view.
Here in this example I am using delete buttons inside the GridView called “Delete”
Let us see how it works.

 

ASPX Code:

<asp:GridView ID="GrdProducts" runat="server" AutoGenerateColumns="False" ShowFooter="true"
DataKeyNames="ProductID" OnRowCommand="GrdProducts_RowCommand">
                <Columns>
 <asp:TemplateField HeaderText="Id">
       <ItemTemplate>  
<asp:label ID="lblProductId" runat="Server" Text='<%# Eval("ProductID"%>' ></asp:label>   
        
        </ItemTemplate>
  </asp:TemplateField>
  <asp:TemplateField HeaderText="Product">
        <ItemTemplate>       
   
   <asp:TextBox ID="txtProductName" runat="Server" Text='<%# Eval("ProductName"%>' ></asp:TextBox>
        </ItemTemplate>
  </asp:TemplateField>
             <asp:TemplateField HeaderText="Product Price">
                 <ItemTemplate>       
<asp:TextBox ID="txtProductPrice" runat="Server" Text='<%# Eval("Price"%>' ></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
    
      <asp:TemplateField HeaderText="Delete?">
           <ItemTemplate>
                <asp:LinkButton ID="lnkDelete" runat="Server" Text="Delete" OnClientClick="return confirm('Are you sure to Delete the record?')" CommandName="Delete"></asp:LinkButton>
          </ItemTemplate>
     </asp:TemplateField>
        </Columns>
    </asp:GridView>


OnRowDeleting event is used to delete selected record:

Delete products using OnRowDeleting Event

      protected void GrdProducts_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
   {
         var dataKey = GrdProducts.DataKeys[e.RowIndex];
         if (dataKey != null)
         {
   {
            var productId = Int32.Parse((dataKey.Value.ToString()));
            //Here delete logic
            var isDeleted = ProductDataAccess.DeleteProduct(productId);
            
            if (isDeleted)
            {
               BindGrid();
            }
         }
      }
            {
               BindGrid();
            }
         }
      }

Actual Data:

 

 

 

 

What is CLR, CTS, CLS and MSIL?

What is the CLR?

CLR is runtime environment provided by Microsoft with .net framework to make development process easier. Code we develop with CLR is called managed code

 

CLR stand for Common Language Runtime.
 

CLR is set of resources provided by Microsoft:


1.  Security model
2. Type system
3. All .NET base classes
4. Many .NET framework classes
5. Development, debugging, and profiling tools
6.  Execution and code management
7.  IL-to-native translators and optimizers
 

Advantages using CLR:

1.  Performance improvements.
2.  Cross-language exception handling
3.  Cross-language integration
4.  Enhanced security
5.  Versioning and deployment support
6.  Debugging and profiling services
7.  Support for custom attributes
8.  A simplified model for component interaction
9.  Garbage Collection
10.  Use of delegates instead increases type safety and security instead of function pointers.
11.  Language features such as inheritance, interfaces, and overloading for object-oriented programming

 

What are the CTS?

Common type system check how types are declared ,used and managed in common language run time.

CTS standardize types for cross-language integration.

 

CTS stands for Common type system

 

The CTS performs following functions:

1. Enable cross-language integration, type safety, and high-performance code execution.
2. Provide OOPs model for multiple language implementation.
3. Defines rules that all languages follow it, which helps ensure that objects written in different Languages can interact with each other.
4. provides a library that contains the primitive data types (such as Boolean, Byte, Char, Int32, and UInt64) used in application development.

 

This is the range of types that the .NET run time understands, and therefore that .NET applications can use.. The CTS is a super set of the CLS(Common language specification).

Example:

Declaration of int variable in VB converted into int32 in C#, System.int class ensures that type safety.

 

What is the CLS?

CLS is nothing but set of guideless provided by Microsoft that language to follow so that it can communicate with other .NET languages in a seamless manner. It was always a dream of Microsoft to unite all different languages in to one umbrella and CLS is one-step towards that.

CLS stands for common language specification.

CLS rules helps to enhance and ensure language interoperability

Better understanding with Example:

CTS are base library which contains all type information like int32, int64, Boolean etc.

For example CLR understands only type int32 type, CLS guidelines maps two different languages types(C# int and VB integer) into int32.

For example common helper class for database application written in C# and database application written in VB and declared following variable.

VB declaration:

Dim roleId As Integer

Same variable now you can declare in C# helper class.

C# declaration:

public int roleId;

 Compiler reads the associated base type of int(C# Integer in VB) is Int32 from CLS guidelines and convert it to Int32.

What is IL?

MSIL stands for Microsoft Intermediate Language. We can call it as Intermediate Language (IL) or Common Intermediate Language (CIL).

A.NET programming language (C#, VB.NET, J# etc.) does not compile into executable code; instead it compiles into an intermediate code called Microsoft Intermediate Language (MSIL).

 MSIL

 During the compile time, the compiler convert the source code into Microsoft Intermediate Language (MSIL) .MSIL is a CPU-independent set of instructions that can be efficiently converted to the native code.

MSIL TO JIT:

During the runtime the CLR Just In Time (JIT) compiler converts the Microsoft Intermediate Language (MSIL) code into native code to the Operating System. When a compiler produces Microsoft Intermediate Language (MSIL), it also produces Metadata. The Microsoft Intermediate Language (MSIL) and Metadata are contained in a portable executable (PE) file .

Microsoft Intermediate Language (MSIL) includes instructions for loading, storing, initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations Just In Time Compiler

Processor execution:

The computer's processor executes the native code.

 

 

MSIL in .net

What is IL?

MSIL stands for Microsoft Intermediate Language. We can call it as Intermediate Language (IL) or Common Intermediate Language (CIL).

A.NET programming language (C#, VB.NET, J# etc.) does not compile into executable code; instead it compiles into an intermediate code called Microsoft Intermediate Language (MSIL).

MSIL:

During the compile time, the compiler convert the source code into Microsoft Intermediate Language (MSIL) .MSIL is a CPU-independent set of instructions that can be efficiently converted to the native code.

MSIL TO JIT:

During the runtime the CLR Just In Time (JIT) compiler converts the Microsoft Intermediate Language (MSIL) code into native code to the Operating System. When a compiler produces Microsoft Intermediate Language (MSIL), it also produces Metadata. The Microsoft Intermediate Language (MSIL) and Metadata are contained in a portable executable (PE) file .

Microsoft Intermediate Language (MSIL) includes instructions for loading, storing, initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations Just In Time Compiler

Processor execution:

The computer's processor executes the native code.