Monday, June 25, 2007

Color Picker control in ASP.NET

      Recently I have to use color picker control in my project so that each user has its own UI. The best color picker example I found on blogger.com. This inspired me to create my own color picker control. 

      You can find the user control here.

Happy Programming !!

Wednesday, June 20, 2007

Create/Delete/View Virtual Directory or Website in IIS using C#

     Hi, in my previous atrlicle I show how to create virtual directory. Here I have attched the code for creating , deleteing or listing virtual directories or website in IIS using ASP.NET and C#. 

    Code uses DirectoryService name space which allows access to Internet Information Services (IIS). The System.DirectoryServices namespace also provides access to the Active Directory. The classes in this namespace can be used with any of the Active Directory service providers including Internet Information Services (IIS), the Lightweight Directory Access Protocol (LDAP), the Novell Directory Services in NetWare (NDS), and WinNT.

   Following softeares are required for this project to run successfully.

   * IIS 5.1 or later version
   * Dotnet SDK 2.0
   * Admin access to server on which IIS in installed

     You can download the Source Code  here. Once you download the file pls change the extension from “doc” to “zip”.    

        Sorry for uploading the code this way. I do not found any option to upload zip file. :)

Happy Programming !!

Tuesday, June 19, 2007

PNG issue in IE 6 or previous version

      In my recent project, I found a starnge behaviour of PNG file. We have created PNG files with transparent back ground for logo. Everything is working fine in IE 7 , Opera, Firefox and even in Safari. However when we saw the image in IE 6 we found a problem that it does not display as transparent back ground.

    Here is the PNG file with transparent back ground. Just save it to your PC and than open it in IE. If you have IE 6 than you can see light blue back ground and if you have IE 7 it will be transparent.

    Below is the html page that display png image.

<html>
<head>
<title>PNG in IE: JS Inc File </title>

<!-- Add below 3 lines in Head section as shown here. -->

<!--[if lt IE 7]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]-->

</head>
<body>
<div class="content">
<h1>PNG in Windows IE: Inc File Demo </h1>
<p>IE now renders this PNG image properly. View the source to see how
to use the JS Include file method.</p>
<img src="AboutUSIcon.png" alt="a PNG logo" />
</div>
</body>
</html>

Fig (1) HTML page that shows PNG file.

  

   Below is the javascript for "pngfix.js" file.Copy the above JS in new file and name it "pngfix.js". 


var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

if ((version >= 5.5) && (document.body.filters))
{
     for(var i=0; i<document.images.length; i++)
     {
             var img = document.images[i]
             var imgName = img.src.toUpperCase()
             if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
            {
                    var imgID = (img.id) ? "id='" + img.id + "' " : ""
                    var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                    var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='"
                                                  + img.alt + "' "
                    var imgStyle = "display:inline-block;" + img.style.cssText
                    if (img.align == "left") imgStyle = "float:left;" + imgStyle
                    if (img.align == "right") imgStyle = "float:right;" + imgStyle
                    if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                    var strNewHTML = "<span " + imgID + imgClass + imgTitle
                                   + " style=\"" + "width:" + img.width + "px; height:" + 
                                    img.height + "px;" + imgStyle + "" + 
                                   "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                                   + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
                  
                    img.outerHTML = strNewHTML
                   i = i-1
            }
     }
}  

Fig (2) Content of "pngfix.js"" file

Happy Programming !! 

Wednesday, June 13, 2007

Moving Options/values up and down in list box using javascript

         Recently I found a really good article on moving options up and down in list box using javascript. Here is the link.

Happy Programming !!

Moving value from one list box to other list box using javascript

           Recent ly I found a really good article on moving value from one list box to another list box using javascript. Here is the link.

Happy Programming !!

Wednesday, June 06, 2007

Shadow Copying

A fantastic feature of ASP.NET is that the code for a Web site can be changed on the fly without shutting down the Web server. When a Web site's file is changed on the hard disk, ASP.NET detects this, unloads the AppDomain that contains the old version of the files (when the last currently running request finishes), and then creates a new AppDomain, loading into it the new versions of the files. To make this happen, ASP.NET uses an AppDomain feature called
shadow copying.

Shadow Copying Assemblies

Shadow copying allows assemblies that are used in an application domain to be updated without unloading the application domain. This is particularly useful for applications that must be available continuously, such as ASP.NET sites.

The common language runtime locks an assembly file when the assembly is loaded, so the file cannot be updated until the assembly is unloaded. The only way to unload an assembly from an application domain is by unloading the application domain, so under normal circumstances, an assembly cannot be updated on disk until all the application domains that are using it have been unloaded.

When an application domain is configured to shadow copy files, assemblies from the application path are copied to another location and loaded from that location. The copy is locked, but the original assembly file is unlocked and can be updated.

The following list describes how to use the properties of the AppDomainSetup class to configure an application domain for shadow copying.

  • Enable shadow copying by setting the ShadowCopyFiles property to the string value "true".

    By default, this causes all assemblies in the application path to be copied to a download cache before they are loaded. This is the same cache maintained by the common language runtime to store files downloaded from other computers, and the common language runtime automatically deletes the files when they are no longer needed.

A little example

Let's come to the point. How to enable this kind of stuff in your own application? A really simple and short example:

First, write a method to create a new app domain to host the new version of the assembly. As you might know, it's impossible to unload an assembly once loaded in an appdomain, you can only unload the associated appdomain entirely. What the following method does is pretty straightforward: it tells the CLR (assembly loader) where to look for files and then tells it to enable shadow copying by setting the (wrong-typed; a boolean property would have been much better) ShadowCopyFiles property to the string "true". Next, an appdomain is created with a unique name using some counter, and the assembly with the functionality is loaded (notice the _ indicates private members of the current class). This method is called when the app starts.

private void LoadAppDomain()
{
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = "c:\\temp";
setup.ShadowCopyFiles = "true";
AppDomain domain = AppDomain.CreateDomain("ShadowCopy
domain " + _domainNumber, null, setup);
_currentAssembly = domain.Load("Server", null);
_domainNumber++;
}

Add items in Drop Down List or List Box using Javascript

        I have seen lots of questions in diffrent forums for adding items in drop down list or list box using javascript. Below is the script for the same.

 

<script type="text/javascript">
function AddItem(Text,Value)
{
// Create Option object
var opt = document.createElement("option");



// Add Option to Dropdown/Listbox
document.getElementById
("DropDownList").options.add(opt);

// Assign text and value to option
opt.text = Text;
opt.value = Value;


}
<script />
        You can use this function in for loop and add more than one Items.
Happy Programming !!

Monday, June 04, 2007

Create Virtual Directory in IIS using C#

            In my recent project I need to create virtual directory in IIS programmatically.  I was searching on net and found a really good article posted by Dipali Choksi. You can find the artilce here. I have copied that article below for my reference.

 

Using System.DirectoryServices;
private
void btnCreateDirectory_Click(object sender, EventArgs e)
{
          string strSchema = "IIsWebVirtualDir";
          string strRootSubPath = "/W3SVC/1/Root" ;

           // you can specify any server name , "localhost" is for example
           DirectoryEntry deRoot =
                        new DirectoryEntry("IIS://" + "localhost" + strRootSubPath);
         try
        {
                 deRoot.RefreshCache();
                 DirectoryEntry deNewVDir =
                                    deRoot.Children.Add("Name of Virtual Directory", strSchema);

                 deNewVDir.Properties["Path"].Insert(0, "Path for Virtual Directory");
                 deNewVDir.CommitChanges();
                 deRoot.CommitChanges();

                // Create a Application
                if (strSchema == "IIsWebVirtualDir")
                           deNewVDir.Invoke("AppCreate", true);
                // Save Changes
                           deNewVDir.CommitChanges();
                
                deRoot.CommitChanges();
                deNewVDir.Close();
                deRoot.Close();
                lblResult.Text = "Virtual Directory "
                                   + ("Name of Virtual Directory"+ "(" + "Path for Virtual
                                             Directory
" + ") has  been created";
       }
      catch (Exception ex)
     {
                lblResult.Text = ex.Message;
      }
}

Fig (1) Code to create virtual directory using C#