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


.

Thursday, September 30, 2010

Google Map in Asp.Net

Introduction

In this article, We are going to Implement Google Map using Asp.net Custom control. It Offers extremely easy and fast way of adding Google Maps API support on your ASP.NET pages.

Lets go through this application step by step

Step 1:

Create a new website >select the programming language as c#> Name the project as GoogleMap

Step 2:

Now lets go to this site and download the DLL from the Recommended download

http://googlemap.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=26521

Step 3:

Now i think that you people have download that ddl.. Now right click on to the website and add reference to arthem.GoogleMap.

Step 4:

Now add this in the Web.config under Pages tag and under Controls.


Step 5:

Now go to this site and give your URL in click on Generate API Key.

http://code.google.com/apis/maps/signup.html

Copy this Key and paste in some document..

Step 6:

Now add this lines of lines of code in .aspx page


We are using Google Marker to indicate your location. Zoom can be set until 9.


Step 7:

Now Lets run this application.. Output will be displayed like this



Download Complete Code

When JavaScript Disabled then redirect noscript page

In this article I have provided some trick for not allowing user to aceess your site when javascript is disabled in browser. It’s very simple and can be implemented with Html tags <META> and <noscript>. Just like script there is <noscript> tag. This tag runs when there is no script enabled for the browser.

<h6> Step1</h6>

create a page where you want to redirect user when JavaScript is disabled. Say we created a page "noscript.aspx". See below code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
document.location.href = "Login.aspx";
</script>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<div style="text-align: center; color: Red; font-family: Calibri; font-size: 12pt; width:400px">
<b>* JavaScript Need To Be Enabled To visit Requested page.</b>
</div>
</div>
</form>
</body>
</html>

User will land on this page when any page is requested with disabled javascript. Specify , on which page you want to redirect user when javascript is enabled in "script>" tag.

<h6>Step2</h6>

In main master page of your site write <noscript> tag.Specify URL = noscript.aspx page we have created.Meta tag will redirect user to given URL (noscript page in this case) when javascript is disabled.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<noscript>
<meta http-equiv="refresh" content="0;URL=noscript.aspx" />
</noscript>
</body>
</html>

Run the application and test it. :)

Wednesday, September 29, 2010

The Repeater control is a data-bound control. It uses templates to display data. It lacks the built-in support for paging, editing and sorting. The Repeater control may be bound to a database table, and an XML file. In this control, the content and layout of list items is defined using Templates. Every Repeater must define an ItemTemplate.
Templates:
There are 5 templates available for a Repeater:
1. Header Template2. ItemTemplate3. AlternatingItemTemplate4. SeparatorTemplate
5. FooterTemplateLet me discuss these templates in details:

HeaderTemplate: It is used to specify an optional header row. It is used to determine the content and layout of the list header.
ItemTemplate :It is used to define the content and layout of items within the list.AlternatingItemTemplate: This template is used for elements that are rendered every second row of data. This allows you to alternate background colors.
SeparatorTemplate: The SeparatorTemplate is rendered between each row.FooterTemplate: It is used to specify an optional footer row. It is used to determine the content and layout of the list footer.
All these 5 templates will be placed within the Repeater .
HeaderTemplateItemTemplate

AlternatingItemTemplate

FooterTemplate

SeperatorTemplate
Link Buttons:

These link Button placed outside of the repeator control



Pagination in Repeater:

Repeater doesn’t support Paging directly. But we can implement Paging in Repeater by using PagedDataSource.

PagedDataSource:
This class comes from the System.Data.Web.UI.WebControls namespace, so we need not import any additional namespace to the page.

This class encapsulates the paging related properties of a data-bound control.
Few properties of PagedDataSource class are as follows:

1. PageSize - This property defines how many records are displayed on a page and it takes an integer (by default it's set at 10).
2. AllowPaging - This property determines whether the paging should be turned on or off (Boolean "true" or "false"). By default it is set to "false".
3. CurrentPageIndex - This gets/sets the current page number. By default it returns 0.
4. PageCount - Returns the total number of pages that are available.
5. DataSource - The value of this property will be the source of the data which we want to page.
6. IsFirstPage - Returns "true" or "false". It depends on whether the current page is the first page in the series.
7. IsLastPage -- Returns "true" or "false". It depends on whether the current page is the last page in the series.

Connection Object and Property Decleration follows:


Code for Page_Load Event


Code written in BringData() method


Here I have created an instance of the PagedDataSource class. In the next line, I had set the DataSource In the following line the AllowPaging property is to "true". And on the next line the PageSize property is set to 3.


Contents of the Table tblUser


Output
The output is as follows:


Conclusion
This is all about Repeater Control. Hope u all enjoy reading it. Please provide me feedback on it, if any improvements required.

Paging with Repeater Control

The Repeater control is a data-bound control. It uses templates to display data. It lacks the built-in support for paging, editing and sorting. The Repeater control may be bound to a database table, and an XML file. In this control, the content and layout of list items is defined using Templates. Every Repeater must define an ItemTemplate.
Templates:
There are 5 templates available for a Repeater:
1. Header Template2. ItemTemplate3. AlternatingItemTemplate4. SeparatorTemplate
5. FooterTemplateLet me discuss these templates in details:

HeaderTemplate: It is used to specify an optional header row. It is used to determine the content and layout of the list header.
ItemTemplate :It is used to define the content and layout of items within the list.AlternatingItemTemplate: This template is used for elements that are rendered every second row of data. This allows you to alternate background colors.
SeparatorTemplate: The SeparatorTemplate is rendered between each row.FooterTemplate: It is used to specify an optional footer row. It is used to determine the content and layout of the list footer.
All these 5 templates will be placed within the Repeater .
HeaderTemplateItemTemplate

AlternatingItemTemplate

FooterTemplate

SeperatorTemplate
Link Buttons:

These link Button placed outside of the repeator control

Add Image

Pagination in Repeater:

Repeater doesn’t support Paging directly. But we can implement Paging in Repeater by using PagedDataSource.
PagedDataSource

This class comes from the System.Data.Web.UI.WebControls namespace, so we need not import any additional namespace to the page.



This class encapsulates the paging related properties of a data-bound control.
Few properties of PagedDataSource class are as follows:

1. PageSize - This property defines how many records are displayed on a page and it takes an integer (by default it's set at 10).
2. AllowPaging - This property determines whether the paging should be turned on or off (Boolean "true" or "false"). By default it is set to "false".
3. CurrentPageIndex - This gets/sets the current page number. By default it returns 0.
4. PageCount - Returns the total number of pages that are available.
5. DataSource - The value of this property will be the source of the data which we want to page.
6. IsFirstPage - Returns "true" or "false". It depends on whether the current page is the first page in the series.
7. IsLastPage -- Returns "true" or "false". It depends on whether the current page is the last page in the series.

Connection Object and Property Decleration follows: