Tuesday, July 28, 2009

Embedding JavaScript files in your custom controls

Following steps are used in order to embed the JavaScript file along with the custom control’s dll as an embedded resource:
1. Add the JavaScript file into the project.
2. Right click on the JavaScript file and click on properties. In the properties window change the Build Action Property into the Embedded Resource [default is Content]
3. Modify the AssemblyInfo.cs by adding the following code
[assembly: System.Web.UI.WebResource("NameSpace.FolderName.FileName.js", "text/javascript", PerformSubstitution = true)]
4. Build the project and JavaScript file will be embedded along with the custom controls dll.

Monday, July 27, 2009

How to add toolbox icon for the custom controls in .net?

Following steps are used in order to add the tool box icon for a custom control assembly:

1. Add the .bmp or .ico file for the image by adding an existing item by right clicking on the Solution explorer of the project.

2. The image and the custom control class should be in the same directory.

3. Add the System.Drawing namespace.

4. Add the following line of code above the class declaration

[ToolboxBitmap(typeof(Namespace.CustomClassName), "Image.bmp")]

5. Rebuild and Add with new dll into the Toolbox inorder to get the toolbox icon.

Friday, February 27, 2009

How to get the the Application Path in asp.net applications Irrespective of production or live environment?

1. Create the following class under the App_Code folder.
public sealed class ApplicationPath
{
#region Private Constructor

///
/// Prevents a default instance of the ApplicationPath class from being created
///

private ApplicationPath()
{
}

#endregion

///
/// Gets the current application path
///

///
public static string GetApplicationPath
{
get
{
string port = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
if (port == null || port == "80" || port == "443")
{
port = string.Empty;
}
else
{
port = ":" + port;
}

string protocol = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
if (protocol == null || protocol == "0")
{
protocol = "http://";
}
else
{
protocol = "https://";
}

string p = protocol + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + System.Web.HttpContext.Current.Request.ApplicationPath;
p = p.Trim();
if (!p.EndsWith("/"))
{
p += "/";
}

return p;
}
}
}

2. Use the ApplicationPath.GetApplicationPath to get the application path.