Tuesday, December 28, 2010

Validation of viewstate in web farm

The default ASP.NET settings ensure that viewstate are tamper proof and encrypted. This ensures that any modification of the ViewState either on the client's computer or over the network is detected when the server processes the data.
To provide tamper proof ViewState, the ViewState content is hashed for each response and the hash is compared on subsequent requests. The validation attribute of the machineKey indicates which hashing algorithm to use, and it defaults to SHA1. If the application is installed in a Web farm, it is needed to change the validationKey from the defautl value of AutoGenerate,IsolateApps to a specific manually generated key value
With the default settings shown above, the AutoGenerate setting instructs ASP.NET to generate a random key. The IsolateApps modifier causes ASP.NET to generate a unique key for each application on your server by using the application ID of each application.
The default value is correct for a single server deployment. You will need to change the default settings if your application is deployed in a Web farm. In a Web farm, you must manually generate the validationKey value and make sure that it is the same on all servers in the farm.

Sunday, December 26, 2010

Setting NavigateUrl property will not set the menu item as selected

If NavigateUrl is set for a menu item, when users select this menu item, the menu item will not be set as selected menu item. The only exception is when the menu item data are populated from site map. In all other case, ASP.Net will not set the selected menu item for the application.
The workaround is getting the aspx file name from request's physical path, and then matching the file name to the corresponding menu item, and then programatically set the matched menu item to be selected.
The issue can be repeated by setting a static menu collection in master page, and then navigate to some content page by click a menu item.

Jonathan

Wednesday, December 22, 2010

Postback with dynamically created controls

One of the main challenges with working with dynamically added controls is that these controls must be programmatically added on each postback. That is, you can't just load these controls on the first page load, and then not reload them on subsequent postbacks. Failure to explicitly add the controls on each postback will cause the controls to literally disappear on postbacks. To further complicate things, the point in the page's lifecycle when dynamic controls are added is important if you want to maintain changed values across postback. For example, imagine you had a Web page that displayed a series of input form fields based on the user visiting the page. The idea here would be to allow the visitor enter some values into these custom input form fields, and then submit the form, having the data saved. If the dynamic Web controls are not added at the correct time in the page's lifecycle, the values entered by the visitor will be lost on postback.

Jonathan

Monday, December 20, 2010

MasterPage and Ajax toolkit

If a content page wants to add a control from ASP.Net Ajax toolkit, like the tab control, it can not simply add the control in it. Doing so will causes an error of resource can not be found...
Before adding an Ajax control in content page, the master page must add the script manager that will be shared by all content pages.
In addition, a content page can also include its own javascrpt file by using script manager proxy
Note that both master page and content page need to @register ajaxcontroltoolkit assmemby.

Jonathan

Tuesday, December 14, 2010

Global resource for ASP.Net localization

When adding localization resource in global resource folder, the region-language name must match the predefined ones. Otherwise, when building the project, an error will happen saying "Error 1: the namespace ??? already contains an definition of ???".
For example, adding a resource of "resource.cn.resx" in the project will cause the error. Changing the name into "resource.zh-CN.resx" will fix it.

Jonathan

Monday, December 6, 2010

Use Html table instead of ASP table in content page

When using Visual Studio 2010 to create master and content page for ASP.Net application, within content page, the layout should be managed with html table instead of asp:table. Otherwise, the controls inside the table cell can not be selected in Design viewer, and you will not be able to access the rich properties provided for the ASP.Net server control from the control's property page.

Thursday, December 2, 2010

SmtpClient sends email from Windows Server 2008

SmtpClient can be used to send email from Asp.net application. However the following configuration is required to make it work.
1. Enable SMTP server service from Server manager page by clicking on "Add Feature" link
2. From IIS 6.0 Manger, right click "SMTP Virtal Server #1", and select "Property". Select "Access" tab, clicking "Relay...", and select "All exception the list below".
3. Restart IIS service

Sample code:

MailMessage mail = new MailMessage(m_strEmail, "jonathan_li2000@yahoo.ca");
mail.Subject = "Test subject";
SmtpClient mailClient = new SmtpClient("127.0.0.1");
mailClient.Send(mail);

Notice that once the server gets restarted, by default the SMTP Virtual server needs to be manually started. To automatically start the SMTP service, set the service to start automatically (Start -> Administrative Tools -> Services).

Good luck!

Monday, November 15, 2010

Visual Studio 2010 and SQL Server 2008

When creating ASP.NET 4.0 application using VS 2010 with SQL Server 2008, by default, the ASP.Net 4.0 users does not have the right permission to access SQL server database. Following the below step to assign the proper user.
1. In IIS Manager, go to the website and in the IIS section, select the Authentication icon and open it. You should have a list of authentications. Right-click on Anonymous Authentication and select Edit. this should be set to Application pool identity.
2. Go to the Application Pools and select the pool your website is in. In the listing, you should see the Identity column. It should be set to LocalSystem. If not, then right-click on the pool and select Advanced Settings. Under the Process Model section, set the Identity to NetworkService by clicking the button on the right-side of the text box and in the dialog, using the Built-in account dropdownlist

In addition, the sqlDatasource wizard may get an invalid object error. Apply the following steps to correct the error.
SQL server 2005 uses a "Schema.Table" naming convention that VS2010 SQLDataSource Wizard doesn't pickup. To workaround the issue, instead of "Specify columns from table or view", select "Specify a custom SQL statement...". On the next screen, use the "Query Builder" to generate the SQL statement. It will pickup the schema name.

Javascript: Passing local variable to setTimeout callback as parameter

In javascript, setTimeout is the only way to make asynchronous method call, however there is a limitation to use setTimeout method - the local variable can not be used as parameter in the callback method. The reason is once the callback is invoked by timer, the original method that calls setTimeout already returns and all the local variable are invalid.
A workaround for this issue is using javascript inner function, the outer functions local variable is visible to the inner function, even if the outer function returns.

Function function1( param )
{
var localVar = param + " is a local variable";
Function innerFunc()
{
alert( localVar );
}
window.setTimeout( innerFunc, 1000);
}

Sunday, November 14, 2010

Recursive FindControl for ASP.Net page

Page.FindControl method only checks the input ID from the immedidate child controls, while in most case the caller wants to get the control from multiple level of child control. It is more useful use the following method to find a control from a page.

public Control FindControlIterative(Control root, string id)
{

if (root.ID == id)
return root;
foreach (Control child in root.Controls)
{
Control childCtl = FindControlIterative(child, id);
if (childCtl != null)
return childCtl;
}


return null;
}

Saturday, November 13, 2010

Html server control or ASP.Net server control

When choosing between html server control or ASP.Net server control, one consideration is whether the appliation needs to support browsers with differetn rendering ablity. If so, it is better to choose ASP.Net server controls over html server controls. By doing so, ASP.Net framework can automatically convert any unsupported ASP.Net server controls into compatible html controls based on the target browser's rendering ability.

On the contrary, if html server controls are used, ASP.Net will not make this conversion based on browser's rendering ability.

This is more important when creating web pages targeting mobile browsers.

Saturday, June 19, 2010

ServiceHostFactory in WCF

You may wonder what is the use of ServiceHostFactor class in WCF.

A quick answer is the class is created only for IIS and WAS to invoke a derived ServiceHost implemented by you. Take a look at @ServiceHost used by service.svc file which included the following attributes:

Service = "Service"
Factory = "Factory"
Debug = "Debug"
Language = "Language"
CodeBehind = "CodeBehind"

It only allows you to specify the type of the service, but not the service host. So if you have implemented a derived service host class with some overriden methods , it has not way to let IIS or WAS to invoke it.
A workaround is implementing a ServiceHostFactory class, and in the CreateServiceHost method, it can create the an instance of the derived service host class implemented by you. Note @ServiceHost does allow you to specify service host factory type, and by this, IIS and WAS can hook up with your derived service host class when it creates the service.

Jonathan