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!