Tuesday, November 2, 2010

ASP.NET Control ClientID and UniqueID, Server control ID

When ever a Server control is rendered in ASP.NET it has two different identifiers, one is named as ClientID which is the "id" attribute and second is the UniqueID which is the "name" attribute.

UniqueID of a control gets the unique, hierarchically qualified identifier for the server control. The fully qualified identifier for the server control, this is the ID which is received as key when value is post back.

The output of button and a text box is like this:
<input type="submit" name="btnLoad" value="Load" id="btnLoad" />
<input type="text" name="txtName" value="Hello" id="txtName" />

Here "name" is what we call btnLoad.UniqueID and "id" is what we call btnLoad.ClientID, in HTML input controls "id" is used for client side validation and other java script things, and "name" attribute is used to identify the field when it is post back to server.

These ID's are in a hierarchy, parent control id first and then child control id, like as follows:
<asp:Repeater ID="rptrNames" runat="server">
<ItemTemplate>
<asp:textbox runat="server" ID="txtName"></asp:textbox>
</ItemTemplate>
</asp:Repeater>

The output of text box is like this:
<input name="rptrNames$ctl00$txtName" type="text" id="rptrNames_ctl00_txtName" />

<input name="rptrNames$ctl01$txtName" type="text" id="rptrNames_ctl01_txtName" />

and so on.

Here ctl00 and ctl01 is Id representing each item of the repeater, txtName is the id for control and rptrNames is the id of repeater control.
The hierarchy of controls is like this:
-Repeater control (Parent control)
--Repeater item (Child control of repeater and parent control of text box)
---TextBox inside each repeater item (Child control of repeater item)

Therefore the UniqueID of textbox is "rptrNames$ctl01$txtName" and ClientID of texbox is "rptrNames_ctl01_txtName".

Another thing noticeable here is the separator between the parent and child control ID's, in case of UniqueID it is "$" dollar sign and in case of ClientID it is "_" underscore. This are fixed control id separators and can be accessed using the read-only property ClientIDSeparator() As Char and read-only property IdSeparator() As Char.

The property ClientIDSeparator() As Char will return (_) underscore and the property IdSeparator() As Char will return ($).

Monday, November 1, 2010

Microsoft Windows, Briefcase

This is a special folder which is used by mobile PC users, this folder provides synchronization feature, file synchronization can be done between briefcase and another folder. The briefcase is infact just a folder, but provides some additional features.

The briefcase folder contains two files one "desktop.ini" and another "Briefcase Database", both these files identify a briefcase folder.

The "desktop.ini" file will look like this:

[.ShellClassInfo]
CLSID={86BAC831-52A0-1069-A2E4-08002B30309D}
RunWizard=0
[Briefcase]
Database=Briefcase Database

Whereas the "Briefcase Database" is a binary file and is used for synchronization process.

Monday, May 31, 2010

Visual Studio 2010, Build error, Build (web): Object reference not set to an instance of an object

When upgrading a from previous version, Crystal report build provider will cause builder error, in ASP.NET 4 crystal reports are also pre-compiled using build provider for (.rpt), when compiler tries to pre-compile reports build in previous version, a build error "Object reference not set to an instance of an object" is thrown and build fails.

Possible work arround is to remove the build provider for (*.rpt) in your web.config:

There will be following construct under section:

<buildProviders>
<add extension=".rpt" type="CrystalDecisions.Web.Compilation.RptBuildProvider, CrystalDecisions.Web, Version=14.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
</buildProviders>

We can change this to:

<buildProviders>
<add extension=".rpt" type="CrystalDecisions.Web.Compilation.RptBuildProvider, CrystalDecisions.Web, Version=14.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
<remove extension=".rpt"/>
</buildProviders>

What we have done is removed the build provider for (.rpt), in this way the crystal reports will remain as it is and no build error will show.

Thursday, May 27, 2010

Object Oriented Javascript (Part-1)

We can develop object oriented JavaScript using function method, in JavaScript functions are the main constructs that can be called or referenced. To write object oriented reusable JavaScript libraries we can utilize the similar function which use every day.

JavaScript function:
At this point i assume that we know what a function is, for quick example it is as follows:
<script type="text/javascript">
//Declare a JavaScript function
function helloWorld()
{
alert('This is my first function.');
}

//calling a JavaScript function
helloWorld();
</script>


JavaScript Class
There is no syntax to declare classes in javascript, but purpose of object class is to encapsulate data and provide some attributes or methods for outer world. This can still be achieved in JavaScript using the same function.
Functions in JavaScript can be instantiated, which means we can create multiple copies of a JavaScript function the same way as we can create instances of a class.

There are different ways to do that, the simplest way is as follows:

<script type="text/javascript">
//Declare a JavaScript Class
function Car(car_color)
{
//Initialize attributes with default values
this.Color = car_color;
this.Model = '';
this.Make = '';
//Methods
this.getModel = function() {
return this.Model;
}
this.setModel = function(car_model) {
this.Model = car_model;
}
}

//Instantiate
var car1 = new Car('Red');
var car2 = new Car('Black');
//Set attributes
//Car1
car1.Make = 'Honda';
car1.setModel('2009');
//Car2
car2.Make = 'Ford';
car2.setModel('2008');
//Call instance method
alert(car1.getModel());
alert(car2.getModel());
</script>


In the above example we can see how two instance of car were created, initialized and accessed in the JavaScript. This simple approach can help in creating reusable code which is encapsulated in a function, this way JavaScript is more manageable and help a lot when you are doing complex scripting.