Monday, January 21, 2008

Javascript - Textbox cursor position at last while focusing

Create a new function in your tag in the HTML section:

function SetEnd (TB)
{
if (TB.createTextRange)
{
var FieldRange = TB.createTextRange();
FieldRange.moveStart('character', TB.value.length);
FieldRange.collapse();
FieldRange.select();
}
}

Create a new onfocus JavaScript event in your asp text box tag (asp:textBox)
that calls the SetEnd function above:

Existing text

Make sure you keep your existing code that calls the SetFocus function as it
is:

private void SetFocus(System.Web.UI.Control ctrl)
{
string s = "
";

RegisterStartupScript("focus", s);
}

Don't forget to call the above SetFocus() function in your Body tag on page
load .....( )

Monday, November 5, 2007

Speed Test: Switch vs If-Else-If

The .NET framework and the C# language provide two methods for conditional processing where multiple discrete values can be selected from. The switch statement is less flexible than the if-else-if ladder but is generally considered to be more efficient.

Test Description
Purpose
This test was designed to compare the execution time of the switch statement and the if-else-if ladder code structure when used to select a match from a discrete series of values. The if-else-if ladder provides much greater flexibility than is used by the test but, when being used to provide matching functionality to switch, this does not need to be considered. The two conditional processing structures are described in the article, C# Program Flow Control: Conditional Processing.

Test Process Functionality
It was important that the speed test for the two statements provided exactly the same functionality in both cases. For each test, an integer value between zero and nineteen was generated. This integer value was then compared against nineteen possible selections using if commands and case statements. Where the integer value generated was nineteen, this was not matched and instead processed using the final else statement or the switch command's default case.

In many programming situations, a selection of this kind is very likely to generate a match. However, for completeness, a second test was performed. This test followed the same functionality but using an integer value that did not match any of the possible values; it therefore always used the final else statement or the switch command's default case.

Looping
The speed of execution of a large if-else-if ladder or select statement is too fast to me accurately measured. To make measuring possible and to reduce anomalies, a loop was constructed and the test code executed repeatedly. For each test, the loop completed one billion (1,000,000,000) iterations.

The looping function for the first test was used to generate the integer value to match. For each iteration in a for loop, the loop control variable was used with the modulus operator to create a comparison value between zero and nineteen.

In addition to executing the test with value-matching and the test without value-matching, the loop code was executed with no internal test code. Two versions of empty loop were used, one with the number generation code and one without. These two tests provided baseline timings that could be subtracted from the actual test times.

Timing
The timing of the tests was controlled automatically using the Stopwatch class. Each test was performed repeatedly and the mean average of the results calculated.

Code
Click this link to download the code for the class used to perform the tests.

Test Conditions
Hardware
The test results included below are those produced using an Athlon64 3200+ with 4GB RAM. These tests are indicative of further relative test results that were carried out on a variety of equipment including:

IBM ThinkPad R51 notebook with a 1.6GHz processor and 2GB RAM
JVC MiniNote notebook with a 1GHz processor and 768MB RAM
The tests were executed using three operating systems, each with the latest service packs and patches. These were:

Windows XP
Windows Server 2003 R2
Windows Vista Ultimate
In each test, the software was compiled as a .NET framework 2.0 console application in three configurations:

Compiled for 32-bit processors only using Visual Studio 2005 Team Edition
Compiled for 32-bit or 64-bit processors using Visual Studio 2005 Team Edition
Compiled using Visual C# 2005 Express Edition
Results
Raw Results
This table shows the timings for the empty loop, if statement and switch statement for one billion iterations rounded to the nearest millisecond. The two columns show the results for the loops where matching of the comparison value occurs and for the non-matching tests.

Matching Non-Matching
Empty Loop 27.3s 5.0s
Switch Statement 43.0s 5.0s
If Statement 48.0s 5.1s

Adjusted Results
This second table shows the results for the two key test types, adjusted to remove the delay created by the looping mechanism.

Matching Non-Matching
Switch Statement 15.7s 0.0s
If Statement 20.7s 0.1s

Conclusion
The results show that the switch statement is faster to execute than the if-else-if ladder. This is due to the compiler's ability to optimise the switch statement. In the case of the if-else-if ladder, the code must process each if statement in the order determined by the programmer. However, because each case within a switch statement does not rely on earlier cases, the compiler is able to re-order the testing in such a way as to provide the fastest execution.

Monday, October 29, 2007

BrowserCaps and other Browser

http://slingfive.com/pages/code/browserCaps/

ASP.Net -- its Request.Browser object is useful for easy browser detection. Request.Browser uses the section of the machine.config XML file (and the optional web.config XML file) to match the current browser's UA (User-Agent) string with its matching capabilities.

problem 1: just like ASP's browscap.ini before it, nobody official is updating this info. Microsoft pawns the job off on Cyscape.com, which doesn't care about doing the world a service (it's busy selling its competing BrowserHawk product instead). As a result, machine.config is already woefully out-of-date and unaware of newer browsers like Mozilla/Firefox, Netscape 7, Safari, and Konqueror, so it tells Request.Browser that they are "Netscape 5" (though Safari and Konqueror are wholly unrelated rendering engines).

problem 2: I went searching and searching for a newer BrowserCaps section updated by someone else. I found some possibilites (linked below), but I didn't like that they don't identify Netscape as Gecko (extrapolating the Netscape brand's marketing version from the real Gecko engine's version). Since Gecko is the rendering engine underneath, that's what we webdevs should care about.

problem 3: By default, Opera lies to web servers that it is Internet Explorer. It sends a UA string like "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera x.x".

solution: ...so I rolled my own BrowserCaps section. I swiped a starting point (also linked below), and warmed up my RegEx chops for the rest. Besides identifying Netscape 6+ and Mozilla/Firebird as Gecko, my update also fixes detection of Konqueror (aka KHTML) and Safari (aka AppleWebKit), and detects Opera as itself. Finally, it (now) also corrects the EcmaScriptVersion and TagWriter properties for Opera 6+.

files and demos
updated BrowserCaps section: with tabs or with spaces
sample page to test browserCaps update: code to copy or demo running on my web server
NEW: sample web.config file with updated browserCaps section: view or download

Installation

For both the web.config and machine.config files, the element goes inside the element (inside ), like so:

configuration>
system.web>
browserCaps>
...
/ browserCaps>
/ system.web>
/ configuration>


Warning: Machine.config is already full of other important code. Do not remove or replace any of it! Just insert the browserCaps contents (contents only!) into the end of the existing browserCaps section.

ifram

http://iframe.in/
Nice site to generate code for different format like

HTML Encoder
JavaScript Encoder
Redirect Generator
IFrame Generator

Friday, October 26, 2007

Connect MAC / Linux from Windows

Connect MAC / Linux from windows with in a network.
Install Ultra VNC (http://www.uvnc.com/)
and connect through Mac / Linux IP.
Make sure that Remote connection is enabled on MAC.