Here's a class I've created that allows you to specify the behavior of the enter key on all controls in your form.
EDIT: I've also added a fix for a case where you want to click a link rather than a button.
public class EnterKeyHandler
{
/// <summary>
/// When the user presses the "enter" key while control_to_watch has focus, call click on button_to_submit
/// </summary>
/// <param name="control_to_watch"></param>
/// <param name="button_to_submit"></param>
public static void ClickButtonOnEnter(WebControl control_to_watch, WebControl button_to_submit)
{
string js = @"
if (event.which || event.keyCode) {
if ((event.which == 13) || (event.keyCode == 13)) {
if ($get('@id@').href) {
eval($get('@id@').href);
} else {
$get('@id@').click();
}
return false;
}
} else {
return true;
}";
control_to_watch.Attributes.Add("onkeydown", js.Replace("@id@", button_to_submit.ClientID));
}
/// <summary>
/// When the user presses the "enter" key while control_to_watch has focus, set focus to next_focus
/// </summary>
/// <param name="control_to_watch"></param>
/// <param name="button_to_submit"></param>
public static void NextControlOnEnter(WebControl control_to_watch, WebControl next_focus)
{
string js = @"
if (event.which || event.keyCode) {
if ((event.which == 13) || (event.keyCode == 13)) {
$get('@id@').focus();
return false;
}
} else {
return true;
}";
control_to_watch.Attributes.Add("onkeydown", js.Replace("@id@", next_focus.ClientID));
}
| permalink | related link |




( 2.9 / 353 )This is something I don't like, but I'll explain how to do it anyway.
Some clients want websites where people can't select your text and can't right click on your images.
I think this is silly; it doesn't really protect your website. For example, if anyone wants to copy your content, they can still take screenshots or view your HTML source. Screen scrapers can still save the contents of your web browser to disk.
However, recently I was building an app that involves lots of mouse movement, and I noticed that once in a while you'd select text if your mouse was moving when you clicked. You'd get a huge block of white-on-blue text in the middle of a page makes it seem like something is broken.
I researched a little bit and found a way to disable this, so I present this to you here. First, edit the body tag of your HTML document with these javascript handlers:
<body oncontextmenu="return false;" ondragstart="return false;" onselectstart="return preventselect();" onmousedown="if (typeof event.preventDefault != 'undefined') {
event.preventDefault();
}"
Next, define this function somewhere in your HTML page:
function preventselect()
{
var el = event.srcElement;
return (el.type == 'text');
}
This "preventselect" function allows the user to still select text if it's within an input box. You might want to modify this function to allow text selection in whatever cases you prefer.
| permalink | related link |




( 3 / 208 )I recently started to see this error message in my Visual Studio .NET 2008 project.
"The following module was built either with optimizations enabled or without debug information"
This didn't make sense, because I was debugging and doing just fine. I found some various bits of advice online for this problem:
1) Clear the "read-only" flag from your "bin" directory
2) Delete your project references and re-add them in the correct order.
3) Rebuild your solution
Sounds easy? The real problem was that I was adding projects in the wrong order that had cross dependencies. For example, I have a solution W which includes libraries A, B, and C. Library A includes library C. What I needed to do was to delete all the references and add them from the ground up. That meant I did this:
1) Right click on my solution W and select "Property Pages".
2) In the references page, delete all of my project references.
3) Add a reference to the project for library C.
4) Add a reference to the project for library B.
5) Add a reference to the project for library A (which needs C, which finds it already exists, which then doesn't need to add a faked reference).
6) Recompile.
Wild!
| permalink | related link |




( 2.9 / 197 )So recently I started getting the error "Sys.Application is null or not an object" when I debug my .NET website application. A javascript error? Since when do applications die on a javascript error?
I found a few references on the web to similar problems. But the best solution seemed to be to create a blank website project and line-by-line compare the web.config file with my broken one. The error was in fact in the document's root tag. Here was the error:
Broken File:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
Corrected File:
<configuration>
Apparently it had something to do with a long lasting project that got upgraded from 2.0 to 3.5.
| permalink | related link |




( 2.9 / 198 )So I have two objects in .NET. They are loaded from my database with a bunch of values as members (fields, in .NET parlance). I'd like to know if the two objects are identical.
In .NET, two objects only == each other if they are the same reference. So a pointer to string A won't equal a pointer to string B if the contents of the two are the same but they reside in a different place in memory. Instead, you must do this:
string a = "test";
string b = "test";
if (a.Equals(b)) { something }
This is all well and good, but what happens when I have two RegionObjects loaded from my database? One way to do this would be to write a custom Equals function that looks like this:
class RegionObject
{
public string country;
public string postal_code;
public override bool Equals(RegionObject obj)
{
if (country.Equals(obj.country) && postal_code.Equals(obj.postal_code)) return true;
return false;
}
}
This works great until I start adding new fields to the RegionObject. Then, I have to remember to add a new test each time I add a new field. I don't like that.
Instead, here's a generic function that gets the job done for any compound object using .NET reflections:
public static bool AreEqual(object o1, object o2)
{
// Make sure both classes are of the same type
Type _t = o1.GetType();
if (!o2.GetType().Equals(_t)) return false;
// Compare all properties and ensure they're the same
PropertyInfo[] props = _t.GetProperties();
for (int i = 0; i < props.Length; i++) {
object value1 = props[ i ].GetValue(o1, new object[] { });
object value2 = props[ i ].GetValue(o2, new object[] { });
if (!value1.Equals(value2)) {
return false;
}
}
// Compare all fields (members) and ensure they're the same
FieldInfo[] fields = _t.GetFields();
for (int i = 0; i < fields.Length; i++) {
object value1 = fields[ i ].GetValue(o1);
object value2 = fields[ i ].GetValue(o2);
if (!value1.Equals(value2)) {
return false;
}
}
// Got this far - they must be identical!
return true;
}
| permalink | related link |




( 3 / 78 )Back Next

Calendar



