Skip to main content

Posts

Showing posts from September, 2007

How to avoid unexpected postback after pressing Enter in textbox

Problem
You fill a form with many input fields. After filling first textbox you mechanically press Enter and see that the page are submitted to the server. Inasmuch as the rest of the fields stay blank the result of this submit is either saving incomplete data or a few validators of type "Field XXX is required." are shown. The well-known situation, is not it? It is not very good to allow such behaviour in a web application, especially because it easily can be corrected.

Of course, there are situations when this is useful (e.g.: you have a single search box and after typing a search keyword it is very convenient to start search by pressing Enter). But in the most cases such behaviour just
annoys visitors.

Solution
Surprisingly, but this inconvenience caused by built-in browser conveniences. :)

Convenience #1. If a form contains only textbox then regardless of the submit button presence pressing Enter will send the form to the server.
http://msdn2.microsoft.com/en-us/library/ms5352…

How to Register Stylesheet Created in Runtime

Here is an example from MSDN:// Create a Style object for the section of the Web page.
Style bodyStyle = new Style();
bodyStyle.ForeColor = System.Drawing.Color.Blue;
bodyStyle.BackColor = System.Drawing.Color.LightGray;
// Add the style to the header of the current page.
Page.Header.StyleSheet.CreateStyleRule(bodyStyle, this, "BODY");

It is the simple and handy method but has a few flaws.
1. Output is far from optimal. For example, following code that adds a bordersomeStyle.BorderColor = System.Drawing.Color.Red;
someStyle.BorderStyle = BorderStyle.Solid;
someStyle.BorderWidth = new Unit("1px");produces this outputborder-color:Red;border-width:1px;border-style:Solid;
instead ofborder:Solid 1px Red;
It has no matter if you don't pay attention to the size of pages you develop, but it is taken into consideration if you take into consideration your visitor's needs (sorry for tautology). But it's a trifle, of course. I mentioned it just for completeness. There is a …