Thursday, October 7, 2010

How to get virtual path from physical path in ASP.NET?

Let's do the opposite of what Server.MapPath does. Is not so hard. Just a c# method and 4 lines of code.


We all know that in ASP.NET the method Server.MapPath give us the physical path given a virtual path, right? Now, you might need to do just the opposite… why not? I googled a lot on this, but found no suitable and/or pretty solution so I just coded this method in c# (yes, I love C#). It works fine for me, hope you can find it useful too.

public string GetVirtualPath(string physicalPath)
{
string rootpath = Server.MapPath("~/");
physicalPath = physicalPath.Replace(rootpath,
"");
physicalPath = physicalPath.Replace(
"\\", "/");
return "~/" + physicalPath;
}

Converting this C# method to VB.NET is quite easy, so no need for that.

Partial Class

Visual Studio .NET 2005, ASP.NET 2.0, shipped with a lesser-known functionality called a “Partial Class”. A Partial class is a class defined in 2 or more files. Each source file contains a section of the class which will combine when the application is compiled.

In previous versions of Visual Studio classes are confined to a single file. It was not possible to split up a single class into multiple files. In order to split up a class we have to use the keyword “Partial”. The purpose of the Partial Class is to allow more than one programmer to write code for the same class, at the same time.

Example




In the above example, the Employee class is split into 2 parts. The first part defines the GetName() method, and the second part defines the GetAge() method. When you compile this program both parts will be combined and compiled.

The important point to remember is both sections use the keyword Partial and the public access modifier.

Points to considered on creating Partial Class

  • All the parts must use the partial keyword.
  • During compile time, all the parts should be available to form the final class.
  • All the parts should have the same access modifiers. – Public, Private, Protected and etc.,
  • Any member declared in the partial class is available to all other parts.
  • If any part is abstract, the entire class is abstract
  • If any part is sealed, then entire class is sealed.
  • If any part has Inheritance, then it applies to the entire class.
  • We can create the Partial Structs, Interfaces and methods in the same way as we create partial classes.
  • Different parts of the partial calls can inherit from different interfaces.

In the above example two parts are declared as public. In the following example we will create one part as abstract.


Output

If you create an object for the Employee class, you will get the following error:

“Cannot create an instance of the abstract class or interface Examples.Employee”.

This is because the second part is declared as abstract, so the whole class became abstract. Hence you cannot create an object.

We can create nested classes as partial class; even when the main class is not partial.

Important Points in Partial Method

  • Partial method declarations must begin with partial keyword.
  • The return type of the partial class should be void
  • Partial methods cannot have the out parameter, but can have the ref parameter.
  • Partial methods can be private but they cannot be virtual.
  • Partial classes cannot be extern.

Dynamically Add a Control to the Page From a User Control

Dear Friend,

actualy this can't be done this way, because at this stage the page has loaded all the controls to build and it can't be modiified at this point as per the error message,you can do the following:
1- in the control page_load function place this line

this.Page.LoadComplete+=new EventHandler(addcont); // adds a function to the vent handler to be done after the page completes the loading

2- add this function which is used in the event handler above inside your control :
public void addcont(Object obj, EventArgs e)

{
Label newlable = new Label();

newlable.Text = "TestText here";
newlable.BackColor = System.Drawing.Color.Red;this.Page.Form.Controls.Add(newlable);

}

inside this function write all the controls you want to add to the page, this here adds a lable with a red background, with some test text in it..

I hope this helps,

please do not forget to mark my post as answer if it helps...

have a nice day,

Create Table From Class Files With There Propertys/ Convert GenericList to DataTable

DataTable dt = GetDataTable(_PageInfo);

private DataTable GetDataTable(PageEntriesInfo _PageInfo)
{
var dt = new DataTable();

foreach (var Info in typeof(CustomElementInfo).GetProperties())
{
dt.Columns.Add(new DataColumn(Info.Name, Info.PropertyType));
}

foreach (var t in _PageInfo.CustomElements)
{
//CustomElements are SubCategory List
var row = dt.NewRow();
foreach (var Info in typeof(CustomElementInfo).GetProperties())
{
row[Info.Name] = Info.GetValue(t, null);
}
dt.Rows.Add(row);
}
return dt;
}

Monday, October 4, 2010

4 steps to increase bandwidth performance for ASPX pages on IIS 6.0

This article is a warm up for my 4th article on .NET best practices. The 4th article was coming out to be too big to accommodate everything. So I have broken down the same in to 2 pieces, this article will show the basic steps needed for that article and the main article will talk in depth about the best practice. In case you have not read my previous articles on best practices below are some links.

Step 1:- Enable compression

Step 2:- Enable metabase.xml edit

Step 3:- Set the compression level and extension types

Step 4:- Does it really work?

Introduction and warning

In this article we will try to understand how to enable compression on IIS 6.0. Once we have enabled compression we will use fiddler to see how bandwidth performance increases due to the same.

Please feel free to download my free 500 question and answer eBook which covers .NET , ASP.NET , SQL Server , WCF , WPF , WWF@ http://www.questpond.com .

Do not jump to a conclusion

As I have clearly mentioned this article will form the base for my 4th article on .NET best practices. So please do not jump to a conclusion and enable IIS compression on production servers. There are issues of CPU performance, compression levels and browser compatibility due to IIS compression enablement. I will be talking about those details later. For now we will just concentrate on how to enable IIS compression and how does it increase bandwidth performance.

Step 1:- Enable compression

The first step is to enable compression on IIS. So right click on websites ? properties and click on the service tab. To enable compression we need to check the below two text boxes from the service tab of IIS website properties. Below figure shows the location of both the checkboxes.

Step 2:- Enable metabase.xml edit

Metadata for IIS comes from ‘Metabase.xml’ which is located at “%windir%\system32\inetsrv\”. In order that compression works properly we need to make some changes to this XML file. In order to make changes to this XML file we need to direct IIS to gives us edit rights. So right click on your IIS server root ? go to properties and check ‘enable direct metabase edit’ check box as shown in the below figure.


Step 3:- Set the compression level and extension types

Next step is to set the compression levels and extension types. Compression level can be defined between 0 to 10, where 0 specifies a mild compression and 10 specifies the highest level of compression. This value is specified using ‘HcDynamicCompressionLevel’ property. There are two types of compression algorithms ‘deflate’ and ‘gzip’. This property needs to be specified in both the algorithm as shown in the below figures.


We need to also specify which file types need to be compressed. ‘HcScriptFileExtensions’ help us to specify the same. For the current scenario we specified that we need to compress ASPX outputs before they are sent to the end browser.

Step 4:- Does it really work?

Once you are done with the above 4 steps, it’s time to see if the compression really works. So we will create a simple C# asp.net page which will loop “10000” times and send some kind of output to the browser.

protected void Page_Load(object sender, EventArgs e)
{
for (int i; i <>
In order to see the difference before compression and after compression we will run the fiddler tool as we run our ASP.NET loop page. You can download fiddler from http://www.fiddler2.com/fiddler2/version.asp .

Below screen shows data captured by fiddler without compression and with compression. Without compression data is “80501 bytes” and with compression it comes to “629 bytes”. I am sure that’s a great performance increase from bandwidth point of view.

Assign Value to Asp.Net TextBox with mode="Password"

Introduction

ASP.NET server control doesn't add/display the value in a password textbox because of security concerns. On most of the web sites at the time of registration password is asked. You must have noticed after submitting the page if you are again redirected to the page due to unsuccessful registration value of password field is lost.This is due to security concerns as everything is gets rendered on the client side So when you view the source in the browser password value will be there.

Solution 1

Add an attribute to textbox.Add this following line of code when you want set the value

TextBox1.Attributes.Add("value", "thePassword")
Above solution will work, but if a user does view source on the form, the password is visible as plain text and that is never good from a security point of view. Rather, I would use Viewstate to hold the password value and if the user doesn't change it then retrieve the value from view state.

Solution 2

But if explicitly you want to set the value of password text box again then use following method in .NET 2.0

txtPassword.Attributes["value"] = Request.Form[txtPassword.ClientID]; //in C#.Net 2.0
Enjoy..

Alt+ Shortcut keys

Have you ever Wondered how someone makes a Heart, Arrow or even a TM trademark symbol with their Keyboard?

All you need to do is Hold down your "ALT" key and press another key on the keyboard to create a symbol.

Here is a list of some you can make. Have Fun!!

· Alt + 0153 - Trademark symbol
· Alt + 0169 - Copyright symbol
· Alt + 0174 - Registered trademark symbol
· Alt + 0176 - Degree symbol
· Alt + 0177 - Plus-or-minus sign
· Alt + 0182 - Paragraph mark
· Alt + 0190 - Fraction, three-fourths
· Alt + 0215 - Multiplication sign
· Alt + 0162 - The cent sign
· Alt + 0161 - Upside down exclamation point
· Alt + 0191 - Upside down question mark
· Alt + 1 - Smiley fsce
· Alt + 2 - Black smiley face
· Alt + 15 - Sun
· Alt + 12 - Female sign
· Alt + 11 - Male sign
· Alt + 6 - Spade sign
· Alt + 5 - Club symbol
· Alt + 3 - Heart
· Alt + 4 - Diamond
· Alt + 13 - Eighth note
· Alt + 14 - Beamed eighth note
· Alt + 8721 - N-ary summation (auto sum)
· Alt + 251 - Square root check mark
· Alt + 8236 - Infinity
· Alt + 24 - Up arrow
· Alt + 25 - Down arrow
· Alt + 26 - Right pointing arrow
· Alt + 27 - Left arrow
· Alt + 18 - Up/down arrow
· Alt + 29 - Left right arrow


.