Sunday, February 08, 2009

ReadOnly and Enabled property of TextBox

One of thing about textbox that confuses me is the Enabled and ReadOnly Property. Both the property makes the text content non editable but there are couple of differences in the way they work.  Here they are:-

Difference No. 1

When we use Enabled Property to false, Textbox’s text is grayed out and we cannot focus on the control and when we use ReadOnly property to true then you will see that the textbox’s content is not grayed out but the content is non editable. 

Difference No. 2

When Enabled = “false” is used, control renders the HTML element as disabled=”disabled” and in case of ReadOnly=”true” then the HTML element is readonly=”readonly”.

I have read on some blogs that disabled controls values are not sent back to the server on postback but that’s not true. If you have disabled any control on client side then only its value will not be sent back to the server. Disabling on client side means either through JavaScript or by adding attributes to the control like

txt.Attributes.Add(”disabled”, “disabled”);

Enjoy…

Wednesday, January 21, 2009

Handle back button of the browser

The "Back" browser button cannot be actually disabled by a web application as the browser security will not allow this.
If we want user to stay on the same page, even if user presses back button, then we can use javascript to achieve this.

<script type="text/javascript" language="javascript">
function Goback()
{
    //window.history.go(+1); 
    window.history.forward();
}
</script>

<body onload="Goback();">

This method will always redirect to the user on the current page itself.

For example, user is on page 1 and user goes to Page 2. Now from page 2 if user press back button of the browser then user will be redirect to the page 2 itself.

window.history contains the collection of the page visited by the user for particular browser session.
window.history.go(+1) and window.history.forward(); are same.

But if we want to display web page expired warning on the click of back button, then we must allow the browser to cache the page. This can be achieved by ASP.NET

/*To not to store the page in cache.*/


Response.ExpiresAbsolute = new DateTime(1900, 1, 1);
Response.Cache.SetCacheability(HttpCacheability.NoCache);

But one problem with this approach is that this will only work when there are some dynamic content in the page. i.e. change any combo or dropdown box value and then try to press back button.

If you can find out better solution then this please let me also know.

Enjoy...



Saturday, January 03, 2009

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack


Few days ago, i come across with this strange error. 
"Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack."
When I debugged my code i found that this error message is coming due to following statment.
Response.Redirect(url); 

I Used the following statement will fix the error.  
Response.Redirect(url,false);
 

Response.Redirect internally calls to Response.End method due to this threadabort exception occurs. The Response.End method ends the page execution and shifts the execution to theApplication_EndRequest event in the application's event pipeline. The line of code that follows Response.End is not executed.

To work around this problem, use one of the following methods:

For Response.Redirect, use an overload, Response.Redirect(String url, bool endResponse) that  passes false for the  endResponse parameter to suppress the internal call to Response.End. For example:

Response.Redirect ("nextpage.aspx", false);

If you use this workaround, the code that follows Response.Redirect is executed.

For Server.Transfer, use the Server.Execute method instead.

Enjoy......

back to top