Thursday, January 29, 2009 displays the cached version of page JavaScript function inherently opens the cached version of the page requested. For the first time the web page will be brought from the server, but for subsequent requests it will display the cached version of the page that was bought first. How to avoid this..? shows the cached version of the page ONLY if the URL requested matches the cached page URL. The best option is to change the URL every time you try to use the statement. This can be achieved by two methods:

  1. Append a dummy queryString with Random value
  2. Append a dummy queryString with DateTime stamp
Out of the two the second method is most reliable, since the DateTime stamp will be unique every time you invoke the statement. Example:

var dt = new Date().toString();
var URLToOpen = '' + dt;'New Page','',URLToOpen);
Your comments are welcome.

Saturday, January 24, 2009

How to get the position of a control using Javascript?

Here is a handy functions to get the posisiton of a control using Javascript:


alert(getElementPosition('txtName').top + ' ' + getElementPosition('txtName').left);

 function getElementPosition(elemID) {
var offsetTrail = document.getElementById(elemID);
var offsetLeft = 0;
var offsetTop = 0;
while (offsetTrail) {
offsetLeft += offsetTrail.offsetLeft;
offsetTop += offsetTrail.offsetTop;
offsetTrail = offsetTrail.offsetParent;
return { left: offsetLeft, top: offsetTop };

We can get the current scroll position of the document using:
var scrolly = typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement.scrollTop;
var scrollx = typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement.scrollLeft;
Hope this helps you...

Friday, January 23, 2009

Virtual Tech Days is back!

Virtual Tech Days.. A series of technical sessions delivered by key note speakers is back ! The agenda for the sessions en comprises of Top web application security bugs, Weaving security in your apps, SDL threat modeling, ASP.NET Ajax fundas, UX fundas, WPF and lot more. Hurry up and register yourself to this exciting event!

You can find more info here:


Wednesday, January 21, 2009

How to Encrypt a Section in Web.Config?

It is often required to encrypt usernames, passwords or even connection string in the web.config file. The usual method to do is using any of the encryption algorithm and saving the encryption Key in the config file. .NET has provided with a classic feature of encrypting an entire section in the web.config file. Using this feature .NET saves the key in the Machine.key file. User ONLY needs to encrypt the section in the web.config file, the decryption of the section is taken care by the .NET framework. Here is a step by step procedure on encrypting a section:

Section in Web.config

<!-- User Credentials -->
<add key ="domain" value ="domain_name"/>
<add key ="username" value ="user_name"/>
<add key ="password" value ="password"/>

Step 1:
Open the Visual Studio Command Prompt in Administrative Mode
Go to Start --> Programs --> Visual Studio 2008 --> Visual Studio Tools --> Visual Studio Command Prompt right click and say "Run as Administrator"

Step 2:
Type the following command:
aspnet_regiis -pef "ImpersonateUser" "D:\SourceCode\RootFolder" -prov "RsaProtectedConfigurationProvider"
The web.config file should be present at the path "D:\SourceCode\RootFolder". The actual command looks like:

Step 3:
Run the above command.
The above command will encrypt the ImpersonateUser section in the web.config file and will save the web.config file at the given location. The encrypted section will look like:

<ImpersonateUser configProtectionProvider="RsaProtectedConfigurationProvider">
<EncryptedData Type=""
<EncryptionMethod Algorithm="" />
<KeyInfo xmlns="">
<EncryptedKey xmlns="">
<EncryptionMethod Algorithm="" />
<KeyInfo xmlns="">
<KeyName>Rsa Key</KeyName>

Most important thing is, while using the web.config key’s in the C# code we DO NOT have to decrypt the section. .NET automatically does it and provides us with the decrypted values.

Hope this helps. Your comments will help us improve :)

Sunday, January 18, 2009

How to convert DataTable to XML in C#?

Following code illustrates about converting a DataTable to XML format. This is often required when passing a DataTable to a stored procedure. We can pass an XML directly to the procedure and process it.

/// <summary>
/// This method is used to convert the DataTable into string XML format.
/// </summary>
/// <param name="dtBuildSQL">DataTable to be converted.</param>
/// <returns>(string) XML form of the DataTable.</returns>
private static string ConvertDataTableToXML(DataTable dtBuildSQL)
DataSet dsBuildSQL = new DataSet();
StringBuilder sbSQL;
StringWriter swSQL;
string XMLformat;

sbSQL = new StringBuilder();
swSQL = new StringWriter(sbSQL);
dsBuildSQL.Merge(dtBuildSQL, true, MissingSchemaAction.AddWithKey);
dsBuildSQL.Tables[0].TableName = "Table";
foreach (DataColumn col in dsBuildSQL.Tables[0].Columns)
col.ColumnMapping = MappingType.Attribute;
dsBuildSQL.WriteXml(swSQL, XmlWriteMode.WriteSchema);
XMLformat = sbSQL.ToString();
return XMLformat;

Your comments are welcome!

How to position an image on Top Right Cornor of the Browser?

Following StyleSheet will position the DIV/IMG tag to the top right cornor of the browser window. This is required when absolute positioning an image is required.

position: fixed;
_position: absolute;
top: 0px;
right: 0px;
clip: inherit;
_top: expression(document.documentElement.scrollTop+document.documentElement.clientHeight-this.clientHeight);
_left: expression(document.documentElement.scrollLeft + document.documentElement.clientWidth - offsetWidth);

You can change the "top" and "right" to "botton" and "left" to appropriately position an image.
Your comments are always welcome!

How to Position HTML DIV tag at the Center?

Following StyleSheet class will position the DIV tag at the center of the screen. This is often required when showing the "Please Wait..." or "Processing..." message while processing Ajax request.

border: 0;
background-color: #6095b3;
height: 10%;
width: 20%;
position: fixed;
_position: absolute;
left: 40%;
top: 40%;
color: black;
z-index: 100;
Your comments are always welcome !

Friday, January 2, 2009

Javascript to Bookmark a website!

Here is a javascript function to bookmark any website:
javascript:bookmark('', 'Sandeep Aparajit Blogs');

<script type="text/javascript">
function bookmark(url,title)
if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4))
else if (navigator.appName == "Netscape")
alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");

Hope this helps :)

Happy New Year!

Wish You All A Very Happy New Year :)