Thursday, December 15, 2011

How to fix, Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code

Problem:

You have a text box in the page with the potential to insert text with HTML tags. When I inserted some text with tags and clicked a submit button to invoke the post back I got the script error.

Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500


In my scenario, I was using ASP.NET 4.0 with Telerik RadAjaxPanel. The first suggestion I found when googling was to turn request validation off. But it did not fix my problem. So to find out the issue, I removed the RadAjaxPanel from the code. That is when I got the next error.


A potentially dangerous Request.Form value was detected from the client


When I googled this issue I found that apart from setting the request validation, if we are using ASP.NET 4.0 we further need to set the httpRuntime attribute requestValidationMode to 2.0.


Reason:


ASP.NET automatically validates incoming HTTP requests to prevent script-injection attacks. It prevents the server from accepting and storing content with un-encoded HTML. However, the developer has the ability to turn this feature off. When request validation is turned off, it is strongly recommended that input data is validated and HTML encode when necessary.

Solution:


To turn off request validation for a page, set ValidateRequest page attribute to false.
ValidateRequest="false"

To turn off request validation for the whole application, set the pages elements validateRequest attribute in the web.config to false.


If you are using ASP.NET 4.0, then set the httpRuntime attribute requestValidationMode to 2.0.


References:


A potentially dangerous Request.Form value was detected from the client
http://www.cryer.co.uk/brian/mswinswdev/ms_vbnet_server_error_potentially_dangerous.htm

Saturday, February 13, 2010

ASP.NET Page Life Cycle

This is an extract from the page life cycle video available at the asp.net site. You can download the video from the here.

PreInit

We can determine if the request is a post back or an initial rendering early in the life cycle by accessing the IsPostBack property in the PreInit event. If the request is a post back, the values of the controls are still not reconstituted from the view state of the control. We can use this event to set global properties of the page.
Ex: Dynamically set a master page or theme, set profile properties using ASP.NET profile system, create or recreate dynamically created controls (controls that are not added during the design time).

Init

Fired after all the controls on the page have been initialized and any skin settings are applied to those controls. We can use this event to read or write any specific control properties.

InitComplete

Since this event is fired after all the controls on the page are initialized we can use this event to perform tasks that depend on the page and controls to be initialized.

PreLoad

Preload event can be used to do any processing that needs to be before the page and the controls get loaded. After the page preload event all the preload events for the controls on the page are fired. Thereafter all the ViewState data gets initialized followed by postback data from the request object.

Load

After the page load event handler gets called, recursively Unload events for all the controls on the page are called as each control is loaded. After the Load and Unload event handlers are called for all the controls in the page, all the other configured event handlers for the controls are called.
Ex: Click event handlers for buttons, TextChanged event handlers for text boxes, Validation controls events

LoadComplete

We can use the LoadComplete event to perform tasks that require all the controls in the page to be loaded and validated.

PreRender

In this point in time all the controls are created in memory and we need to render those controls, so that the markup can be sent to the browser. Before the PreRender event gets fired, the page object calls to ensure that every child object in the page has its PreRender event called, every data bound control that has a DataSourceID property set has its DataBind method called. We can use the PreRender event to make final changes to the content of the page or the controls of the page before it is turned into html for sending to the browser.

SaveSateComplete

Before this event is fired, ViewState for the entire page and its controls are saved. So any changes done to the state of the page and the controls on the page will not be saved and rendered on the browser. We can use this event to perform any logic against the ViewState but not any modifications. Now the page is ready to be rendered.

There is no Render event but the page object will call all the Render methods of all the controls on the page which will render all the types of controls (built-in controls, user controls, custom controls etc.).

Unload

When Page_Unload event is fired, we cannot do anything with the page because the page is already rendered. If we try to access any page elements during this event, exceptions will be thrown.
Ex: We cannot access the Response object

Unload events will be fired for both the page and each individual control on the page. We can use the Unload event to do final cleanup tasks like closing database connections, closing opened files etc.

Tuesday, April 14, 2009

How to change the style of DataGrid columns during the runtime (Apply styles to DataGrid columns at runtime)

Recently I was asked to do a task where it needed to change the style of a DataGrid column item during the runtime. I tried in various ways and finally came up with the solution listed below. The requirement was to change the applied style of a DataGridColumnTemplate and apply a new style. We are using the Telerik RadGrid in our development but I am quite sure that this can be applied to the normal grid as well. The style needed to be applied to a LinkButton which was inside the DataGridColumnTemplate.

I am using the ItemDataBound of the DataGrid control to apply the changes. First I am selecting the particular row to be edited. In this case, the row in which the style needs to be changed. In this example, I am first checking whether the item provided to the event is a GridDataItem. In this example, I am only changing the style of rows, which meets a specific criterion (I am checking whether a particular record is ‘inactive’). Thereafter I am setting the style of the entire row to a new style (item.CssClass = "disabledrow";).

Each DataGridColumnTemplate has a controls collection. We need to select the required control from the collection (if (control.ID == "imgSearchView")). Then cast the control to the required control and apply the style.

LinkButton linkButton = null;
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
if (item["activecolumn"].Text.ToString() == EntityActiveStatus.Inactive.ToString())
{
item.CssClass = "disabledrow";

foreach (Control control in item["viewcolumn"].Controls)
{
if (control.ID == "imgSearchView")
{
linkButton = (LinkButton)control;
linkButton.CssClass = "btnViewGray";
}
}

foreach (Control control in item["editcolumn"].Controls)
{
if (control.ID == "imgSearchEdit")
{
linkButton = (LinkButton)control;
linkButton.CssClass = "btnEditGray";
}
}

foreach (Control control in item["deletecolumn"].Controls)
{
if (control.ID == "imgSearchDelete")
{
linkButton = (LinkButton)control;
linkButton.CssClass = "btnDeactivateGray";
linkButton.ToolTip = "Activate";
}
}
}
}

Please leave a comment or contact me at my email address if you have any concerns.

Saturday, March 28, 2009

MCAD...finally...

It is been a long time since I started my certifications. While I was able to complete three exams on the trot, I could not continue the same trend over time. It’s now close to a year since I started the certification. But I am really happy, that I was able to become an MCAD, even it more time than it should. Hopefully, I am thinking of going for MCSD, which as of now seems a really tough task with just three days left for the exam retirement date.

Thank God...I have finally started blogging...

I wanted to blog for long time. But I never really got started. Finally..today...I started...to blog. I think blogging will not only be fun...but will enable me to learn a lot of stuff...hopefully...I can continue what I started... :D