C# Property Accessors 
Let's say you've got an object and the name of a property you want. How do you get that programmatically? You could easily write something like this:

Animal a;
string propname;

if (propname.Equals("size")) {
return a.size;
} else if (propname.Equals("color")) {
...
}


However, to put it bluntly, that sucks. In Javascript you can just do "Eval("obj." + propname);". How do you do this in C#?

The answer is that we use the System.Reflection library.

using System.Reflection;
...
PropertyInfo pi = obj.GetType().GetProperty(propname);
object o = pi.GetValue(obj, new object[] { });


Now the variable o has the object's property value. Nifty! You can also set the appropriate parameter similarly:

pi.SetValue(obj, value, new object[] { });

  |  permalink  |  related link  |   ( 3 / 201 )
Can't Connect to Windows Server 2003 R2 Computer 
I set up a new computer running Server 2003 R2. For some reason, my Vista laptop couldn't connect to it. Then, I noticed that my Windows XP SP2 desktop also couldn't connect to it. Then I noticed that they couldn't even ping it!

At this point, something was totally wrong. I know that Windows, when you first install it, says "Your computer is in secure mode. Click here to enable outside connections to the Internet." However, I couldn't find this dialog box anywhere. So instead, I had to run the Security Configuration Wizard to enable specific connections.

Egads, SCW is a beast! It's way too complicated and fairly obnoxious. But in the end, it did enable connections. I just had to accept 16 pages of default choices that I didn't take the time to understand.

1. Launch Add/Remove Programs.
2. Select Add/Remove Windows Components.
3. Select "Security Configuration Wizard" and install it.
4. When the install is complete, select Start | Administrative Tools | Security Configuration Wizard.
5. Pick all the default settings, save to disk, and go!


Let this be a lesson to you - be careful with your installs! Don't call it done until you've tested all your key services and made sure they work.
  |  permalink  |  related link  |   ( 3.1 / 155 )
Prevent the spacebar from scrolling the window in Javascript 
I have an AJAX webpage and I'm trying to use the spacebar to trigger a control. Instead, the web browser interprets a spacebar as a "pagedown" key. I don't want this to happen! However, this is kind of tricky. Here's a function that will do the job:

function KillSpaceBar(e)
{
var code;
e = e || window.event;
if (e.keyCode) {
code = e.keyCode;
} else if (e.which) {
code = e.which;
}
if (code == 32) {
e.cancelBubble = true;
e.returnValue = false;

if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
return false;
}
}


Why is it so complicated? First, we need to connect our event with the window event (e || window.event). This prevents IE from keeping the original event in motion.

Next, we have to determine the keycode that was pressed - each browser does this slightly differently, so we look at both keyCode and which. We only want to handle a space - int 32 - so we ignore this event if the result is any other number.

Now, in order to stop this event from propagating, we have to use the cancelBubble variable to prevent IE from continuing to process the result. We also have to set this event's value to false and set the returnValue. Firefox requires the stopPropagation() function. What a mess!

But it works! Don't forget to attach it to your scrolling <div> tag's or your window's "onkeydown" event:

$get('ContentAreaDiv').onkeydown = KillSpaceBar;
window.onkeydown = KillSpaceBar;


In fact, it's helpful to do this every time the page is loaded from AJAX, so we don't lose our event.


Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
function pageLoaded()
{
$get('ContentAreaDiv').onkeydown = KillSpaceBar;
window.onkeydown = KillSpaceBar;
}


There you go!
  |  permalink  |  related link  |   ( 2.9 / 179 )
Check if a variable is defined in Javascript 
How funny. I thought it would be easy - and it is. If you're not sure if a variable is defined in javascript, here's how to check:

if (typeof x=="undefined") {
// variable is undefined
} else {
// variable is defined
}


Yup!
  |  permalink  |  related link  |   ( 2.9 / 244 )
Stretch an HTML element to fill the window in Microsoft AJAX 
Let's say I have an HTML element on my AJAX page, like a <DIV> or a table. I want this HTML element to sit beneath my page's top navigation and extend all the way down to the bottom of the window without stretching the window out to the point it would require a scrollbar.

First, let's place a <DIV> element on the page. This element needs to have a relative position rather than an absolute position so that it can flow correctly beneath my top navigation. I'll give it a default 1x1 size for the moment, and an ID that I can use easily:

<div style="position: relative; width: 1px; height: 1px;" id="BottomSpacer">&nbsp;</div>


Now, when the browser window is resized, I want to stretch this DIV out to the new width. First, let's write a function to determine how tall the window is.

function browserWindowHeight()
{
return (window.innerHeight != null) ? window.innerHeight :
(document.documentElement) && document.documentElement.clientHeight ? document.documentElement.clientHeight :
(document.body != null) ? document.body.clientHeight : null;
}


When we know the height of the document, we can stretch the <div> element to the correct height. We want to first determine its position on the page, using a simple function like so:

function findYPos(obj)
{
var cur = 0;
while (obj.offsetParent) {
cur += obj.offsetTop;
obj = obj.offsetParent;
}
return cur;
}


This is a simplified version of the findBothPos() function I used in a previous article. With these two functions, we can now size our div element to the correct height. However, we want to make sure that we only restretch it if the window height has changed, so let's keep track of the window height ourselves and only do this work if we see a difference. More importantly, when we call this function, we'll tell the window to re-call the function every time something changes by attaching ourselves to the window.onresize event:

var _old_height = 0;
function StretchDiv()
{
window.onresize = StretchDiv;

// Only adjust height if something changed
var h = pageHeight();
if (_old_height != h) {
_old_height = h;

// Resize the content area div
var element = $get('BottomSpacer');
var new_height = h - findYPos(d);
if (new_height < 1) new_height = 1;
element.style.height = new_height + 'px';
}
}


Next thing we need to do is to attach this behavior to the page. The best way to do it is with the window.onresize event. Here's how to attach this using Microsoft AJAX:

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(StretchDiv);


Using the add_pageLoaded event is critical - this tells Microsoft AJAX to call the StretchDiv function every time a successful page request is completed. Since many page requests can adjust the size of your window, this may be necessary for you.

Also, remember to play around with the new_height variable. If your div sits between the header and the page footer, you will also need to subtract the footer element's offsetHeight.

  |  permalink  |  related link  |   ( 4.1 / 541 )

Back Next