Using ClientIDMode Feature of ASP.net 4.0:
Using ClientIDMode Feature of ASP.net 4.0:
The client ID mode feature of ASP.net 4.0 gives the developer a little bit more control over how the ID will be generated. By default all ASP.net controls uses the Inherit as theClientIDMode.
Static Mode:
As the name suggests the static mode can be used to give a constant ID to the ASP.net control. Simply assign ClientIDMode property equal to Static in your control and it will start generating the ID using the static mode.
The implementation below shows how to use the ClientIDMode equal to static for the GridView control.
1 | <asp:GridView ID= "gvCustomers" ClientIDMode= "Static" runat= "server" > |
Now run the application and examine the ID generated by the GridView control. The screenshot below shows that the GridView ID has no affect from the parent control ID.
Let's make things a little bit more interesting by adding a TemplateField column to our GridView control. The implementation is shown below:
01 | <asp:GridView ID= "gvCustomers" ClientIDMode= "Static" runat= "server" > |
02 |
03 | <Columns> |
04 | <asp:TemplateField> |
05 | <ItemTemplate> |
06 | <asp:TextBox ID= "txtName" runat= "server" ></asp:TextBox> |
07 | </ItemTemplate> |
08 | </asp:TemplateField> |
09 | </Columns> |
10 |
11 | </asp:GridView> |
As you can see in the code above we have added a new TextBox control inside the GridView control. Since the default mode of all ASP.net controls is set to inherit this will cause all TextBox controls to create the same ID as shown in the screenshot below:
You might be expecting that the page will result in an error but no error is generated and you will have multiple controls having the same ID. This is not a good practice because it is now extremely hard to select a particular control from inside the GridView control. If you are using JQuery to select the "txtName" element using the ID then it will return you the first occurrence of the element with the ID "txtName".
In the next section we are going to fix the problem of creating multiple controls with the same ID using the predictable option of the ClientIDMode property.
Predictable Mode:
Predictable mode allows to create IDs that are dependent on the ClientIDRowSuffix property of the parent control. If you do not provide a value for the ClientIDRowSuffix then the RowIndex will be used as ClientIDRowSuffix. Take a look at the implementation below where we have set the client ID mode property of the TextBox control to Predictable.
01 | <asp:GridView ID= "gvCustomers" ClientIDMode= "Static" runat= "server" > |
02 |
03 | <Columns> |
04 | <asp:TemplateField> |
05 | <ItemTemplate> |
06 | <asp:TextBox ID= "txtName" ClientIDMode= "Predictable" runat= "server" ></asp:TextBox> |
07 | </ItemTemplate> |
08 | </asp:TemplateField> |
09 | </Columns> |
10 |
11 | </asp:GridView> |
If you run the above code and view the source of the page you will notice that the index number of the row is concatenated with the ID of the TextBox control.
Although the above approach creates unique ID for the TextBox control inside the GridView control but they are still not developer friendly. For this reason you can assign a property from the data source to the ClientIDRowSuffix field. It is a good practice to use a unique ID as a value for the ClientIDRowSuffix so all the controls generated have unique IDs. In the code below we are using the CustomerID as the ClientIDRowSuffix which will be appended with the ID of the TextBox control.
01 | <asp:GridView ID= "gvCustomers" ClientIDRowSuffix= "CustomerId" ClientIDMode= "Static" runat= "server" > |
02 |
03 | <Columns> |
04 | <asp:TemplateField> |
05 | <ItemTemplate> |
06 | <asp:TextBox ID= "txtName" ClientIDMode= "Predictable" runat= "server" ></asp:TextBox> |
07 | </ItemTemplate> |
08 | </asp:TemplateField> |
09 | </Columns> |
10 |
11 | </asp:GridView> |
id=txtName_customerid from db
EX: txtName_1234
EX: txtName_1234
Wednesday, 21 May 2014
Assembly
What is a .NET assembly?
An assembly is the primary building block of a .NET application and can take the form of a dynamic link library (DLL) or executable file (EXE). An assembly is a collection of functionality that is built, versioned, and deployed as a single implementation unit.
What does an assembly contain?
A .NET assembly may contain the following elements:
- Assembly Manifest – Metadata that describes the assembly and its contents (see below)
- Source Code – Compiled into Microsoft intermediate language (MSIL)
- Type Metadata – Defines all types, their properties and methods,classes, interfaces, enums, structs and most importantly, public types exported from this assembly
- Resources – Icons, images, text strings and other resources
The assembly manifest is required; the other elements are optional.
What is an assembly manifest?
An assembly manifest is metadata inside an assembly that describes everything there is to know about the assembly and its contents. The manifest contains:
- Strong Name – The assembly’s name, version, culture, optional processor architecture, and public key (for shared assemblies)
- File Contents – Name and hash of all files in the assembly
- Type List - Types defined in the assembly, including public types that are exported from the assembly
- Resource List – Icons, images, text strings and other resources contained in the assembly
- Dependencies – Compile-time dependencies on other assemblies
- Security – Permissions required for the assembly to run properly
MISL code is executed through CLR (it cannot be directly executed)
.NET 3 types of Assemblies are available,
it is classified as,
1. Private Assemblies
2. Shared Assemblies (public Assembly)
3. Satellite Assembly
it is classified as,
1. Private Assemblies
2. Shared Assemblies (public Assembly)
3. Satellite Assembly
Private Assemblies
A private assembly is used only by a single application and is stored in that application's directory other wise in the application's sub directory.
A private assembly is used only by a single application and is stored in that application's directory other wise in the application's sub directory.
There is no version constraint in private assembly.
If an assembly is copied in to the respective application in which we would like to use is known as local assembly. if any changes made to the copy that will not reflect the copies in other applications.
Public Assemblies
- It resides in GAC, so that anyone can use this assembly. Public assemblies are always share the common.
- It has version constraint.
- This public assembly is stored inside the global assembly cache or GAC.GAC contains a collection of shared assemblies.
- If an assembly is copied into the global place and reference is used from all other applications then this is called public or global assembly..if we want to copy assembly in global place we have to create strong name by using sn.exe.
A satellite assembly is defined as an assembly with resources only, no executable code.
Satellite assemblies are used to build multi-linguistic applications. Application which has built in supportive of more than one human readable language is known as multi-linguistic applications.
- Satellite Assemblies doesn’t contain any Data
- Satellite assembly is containing cultural information.
- Satellite assembly mainly used for to display information based on the Cultural setting of browser or region.
What is the difference between a private and shared assembly?
A private assembly is used only by a single application and is stored in that application's directory other wise in the application's sub directory. The name of a private assembly name must be unique within the application that uses it. There is no version constraint in private assembly.
A shared assembly is used by multiple applications and is typically stored in a global folder known as the Global Assembly Cache (GAC). When building an assembly, a developer must specifically choose to build it as a shared assembly by giving it a cryptographically strong name. For example, the .NET Framework is a collection of shared assemblies.
What is the difference between an assembly and a namespace?
Namespaces are logical, whereas assemblies are physical.
A namespace is a logical naming scheme to group related types. Namespaces can contain other namespaces to form a hierarchy. The “fully qualified name” of a type is its namespace followed by its type name, separated by a period (for example, System.Windows.Forms.Button). Type names must be unique within a namespace, but the same type name can be used in different namespaces.
An assembly is a physical deployment scheme to group related types. An assembly can contain one or many namespaces. A namespace can exist in one or many assemblies.
What is the Difference between Assembly and DLL?
DLL is a dynamically linked library. Although, assemblies are physically equal to DLLs, they are very different internally. It is not possible to maintain consistency between a set of DLLs, but the CLR can maintain consistency between a set of assemblies, because assemblies are self-describing (they contain the list of dependencies internally). Unlike for DLLs, versioning information is enforced for assemblies (by the CLR). Side-by-side deployment (different applications using different versions) is possible with assemblies.
No comments:
Post a Comment