Sadie Cam is live

August 20th, 2009 No comments »

dogs 002I like to keep an eye on one of our dogs, Sadie, as I’m beginning to leave her outside of her cage. In an effort to make sure she doesn’t destroy the room, I set up the Sadie Cam, which is viewable here.

The cam is usually on during work hours. Enjoy.

Macbook Pro Disassembly

August 7th, 2009 No comments »

Presented by Ice-T.

Create Custom Event Handlers & Arguments For Controls

July 28th, 2009 No comments »

I was hoping to get this post up earlier, but got distracted by a couple of things. First, I jacked up my hand trying to separate my dogs from fighting to the point I couldn’t even move my ring finger (long story about the dogs, but I love ‘em). Second, I started a new job at Groove Commerce on Monday. So far, so good. I feel it’ll keep me on my game and give me some nice challenges ahead.

A few entries ago we talked about custom server controls and mentioned how to structure a composite control to allow the ViewState to “pick up” on it’s post back content so that data isn’t re-binded with every call. It was a very basic composite control, but it showed how to add native WebControl objects to the Controls tree to create a basic layout without using a .ASCX design file. This type of control can be added to your app_code directory or manually compiled as an assembly and placed into your bin directory. The advantage of the latter is to feasibly add it to your Visual Studio toolbar by loading the assembly. This time, we’ll add a custom event to our control using a delegate for an event, as well as a custom event argument class to pass along with it.

We’ll take the code from last time, but add several things which are highlighted in green.

  • A line to declare our event delegate variable and the delegate itself.
  • A class which will be used for our arguments.
  • A binded event for the button Click.
  • A line in the button Click handler to invoke the event delegate.
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace RyanControls
{
    public class MyCompositeControl : CompositeControl
    {
        public MyCompositeControl() { }

        // Declare the Updating event of delegate type CustomControlUpdateHandler
        public event CustomControlUpdateHandler Updating;

        protected override void CreateChildControls()
        {
            TextBox txtMyData = new TextBox();
            DropDownList drpMyList = new DropDownList();
            Button btnUpdate = new Button() { Text = "Update!" };

            Controls.Add(drpMyList);
            Controls.Add(txtMyData);
            Controls.Add(btnUpdate);

            if (!Page.IsPostBack)
            {
                drpMyList.Items.Add(new ListItem("item 1"));
                drpMyList.Items.Add(new ListItem("item 2"));
                drpMyList.Items.Add(new ListItem("item 3"));
            }

            // Add a handler for the button click to launch the Updating event
            btnUpdate.Click += new EventHandler(FireEvent);
        }

        // The method that handles the Click event from the button
        // and triggers the Updating event by calling the delegate
        protected void FireEvent(Object s, EventArgs e)
        {
            Updating(s, new CustomControlUpdateEventArgs() { SomeData = "The control was updated!" });
        }
    }

    // Our custom event class that inherits EventArgs. You may use
    // EventArgs as the class if you don't have additional data to pass
    public class CustomControlUpdateEventArgs : EventArgs
    {
        public CustomControlUpdateEventArgs() { }
        public String SomeData;
    }

    // The delegate declaration (or template, as I call it) for the handler
    public delegate void CustomControlUpdateHandler(Object s, CustomControlUpdateEventArgs e);
}

There you have it. If you place this code into a .CS file inside your app_code directory, you’ll be able to include the control on your pages. If you compiled it into an assembly, toss it into your bin directory and feel free to add it to your toolbar in VS or VWD.

<asp:MyCompositeControl runat="server" ID="cntMyControl" OnUpdating="DisplayUpdate" />

By assigning an event handler for the Updating event (the page automatically prepends the “On” for “OnUpdating”) to call a method to handle the update, you can use data contained in the CustomControlUpdateEventArgs object. In this case, there’s only a public string called SomeData.

protected void DisplayUpdate(Object s, RyanControls.CustomControlUpdateEventArgs e)
{
    Response.Write(String.Format("<div>From the event handler: {0}</div>", e.SomeData));
}

When you run the code and click the button, the method will be invoked and the message displayed. Similarly, you can bind the handler programmatically by adding cntMyControl.Updating += new RyanControls.CustomControlUpdateHandler(DisplayUpdate) in your code-behind.

custom_event

So again, while the code isn’t really useful, it shows how to create your own event handlers and custom argument class(es) for a composite server control. The point is to be able to create one or more event handlers, as well as combine those with one or more types of event argument classes. The fields of that particular argument class should be specific to that type of event and it is best not to use one custom event args class to satisfy many types of event argsuments, unless the data being passed into your hander functions generic enough to do so.

Happy coding!

Home Server How-to: Introduction

July 22nd, 2009 2 comments »

I’m going to begin talking a little about setting up your own home server and discuss not only some cool stuff you can do, but how to get the most use out of it. When I mention to people that I have a home server, they snicker. And that’s fine considering that the thought of having a server in your home usually connotates that you’re some hacker that orders pizza online and doesn’t leave the house for days. However, yes, having a some server in their house is kind of a geeky thing to do, but if used effectively, can be really beneficial to those actually using the network. All you need is a spare PC, some ethernet cable, and a spare port on your router.

You can do a lot with a home server. Some things may you may find more interesting depending what stuff you’re into, but here is a broad list:

  • Backups – This is by far the biggest benefit. If you’re not using automated backup software on your computers, you run the risk of a catostrophic failure (usually in the form of a dead or dying hard disk) and losing all your files. With a server, you run scheduled backups one or more times per day to grab those files off your PC or laptop and save them to your server.
  • Media storage – Have a lot of music, videos, or photos? Have a spouse or kids who want access to that content? Instead of sharing one computer, store all your media on a drive connected to the server and allow for anyone to access that content any time from their own computer.
  • FTP server – If you’re on the road a lot or just want access to your files, you can set up an FTP (file transfer protocol) server, allowing you to grab and/or save files to or from your server from anywhere you can steal an internet connection.
  • Remote desktop – If you need to connect to the desktop of your server while you’re away, you can connect to it and view any documents or backups.
  • Web server – While the first two are pretty common, running your own web server involes a bit of skill, including setup of your web application software (e.g., IIS, Apache), configuring your router and firewall, installing databases, etc. This can be a daunting task to someone new, but you can host your own websites, even your WordPress blog, from your server for free! With free DNS services out there, such as Afraid.org, as long as your ISP lets you hang onto your IP, all you have to pay for is the domain name.
  • Host your own email – This one can be tricky, especially when dealing with blacklists. With free email tools out there for Windows, such as hMailServer, you can setup POP or IMAP email for your domain. You can even run backups of your email.
  • Gaming server – Yes, you can have a wicked LAN party and have your server host all the games. Ask your wife about having the party first.
  • Streaming webcam – I used this idea when we got our new dog and I wanted to keep an eye on her while at work. By hooking a webcam, and using some streaming software, I was able to connect to and stream video from the camera.

The concept of having a home server is pretty simple. You have a computer running 24/7 on your home network. It’s connected to your router and can talk to other machines on your network. By “talking” to other computers on your network, it simply mean that it can view their folder contents as well as allow its contents to be viewed by other computers on your network.

For this intro, I just wanted to cover the basics and give an overview of what a home server can do for you. For next time, I’ll discuss how to pick a computer, connect it to your network, and set up shared folders.

Composite controls and maintaining ViewState in ASP.NET.

July 17th, 2009 No comments »

One day you’ll find that you’ve outgrown the native web controls in .NET, or find yourself using a set of controls in a lot of your projects, and feel the urge to implement something….custom. We’ve discussed extending a single, preexisting control to add extra functionality (such as a DropDownList pre-binded with gender data for a form), but what if you want to combine multiple controls, something similar to a user control, and want it to be easily deployable – such as dropping an assembly into your bin directory?

The answer is creating a composite control. A composite control is exactly what it sounds like – it’s a set of controls, added using the Controls.Add(…) method as child controls to the object. You lose out on design-time aspects you would otherwise get by using a user control (using a .ASCX file and inheriting from UserControl), but you gain the ability to compile one or more controls into a single assembly for easy redistribution. You may even add these to your Visual Studio toolbar.

As mentioned earlier, the Controls.Add(…) method is used to add child controls to the control “tree” inside your control. This method is available at any point in the state of your control but you should try to construct your control tree inside the CreateChildControls() method for one major reason – ViewState. If you build your controls inside of Render(), your controls will still load, but during a PostBack, you will lose your ViewState.

This behavior is related to the Page lifecycle and the order in which events fire. During a PostBack, the CreateChildControls() method is fired BEFORE the page’s PreLoad event, which is the event that loads up the ViewState from your last PostBack. The reason this is important is because, when you invoke Controls.Add(…) inside of that method, a special method called TrackViewState() is invoked on your composite control. Once this is called, any binded data will be added to the ViewState. Once it is added to the ViewState, the Page’s PreLoad method can load up the ViewState on a PostBack. This order is very important in maintaining the state of your composite control during PostBack.

Lets play with some code. The following code builds a very, very simple (pretty much useless) composite control. The point is to really show the basic structure and to demonstrate how it relates to maintaining the ViewState for TextBox and DropDownList controls. This control is inheriting from the CompositeControl class, which implements the INamingContainer interface. This marker interface gives the control’s child elements a unique ID (in reference to the parent control’s ID). You could also inherit from WebControl and implement the INamingContainer (System.Web.UI) interface yourself.

using System;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace RyanControls
{
    public class MyCompositeControl : CompositeControl
    {
        public MyCompositeControl() { }

        protected override void CreateChildControls()
        {
            TextBox txtMyData = new TextBox();
            DropDownList drpMyList = new DropDownList();
            Button btnUpdate = new Button() { Text = "Update!" };

            Controls.Add(drpMyList);
            Controls.Add(txtMyData);
            Controls.Add(btnUpdate);

            if (!Page.IsPostBack)
            {
                drpMyList.Items.Add(new ListItem("item 1"));
                drpMyList.Items.Add(new ListItem("item 2"));
                drpMyList.Items.Add(new ListItem("item 3"));
            }
        }
    }
}

Notice that I’m adding ListItem elements AFTER I added the DropDownList to the Controls list. This is an important step. If I had not done this, while I would be able to add items for this first instance of the control, they would be added BEFORE the TrackViewState method was invoked by performing Controls.Add(…). Because of this, they would not added to the ViewState and not be available during PostBack. If I had removed the IF-statement for checking IsPostBack and added items before the Controls.Add(…) method, it would still essentially work during a PostBack – the items would still appear and the selected index would still be correct (the ViewState is maintained for the controls themselves, but not their child controls in this case). However, this is adding the items for each and every time it’s called. If you were grabbing this data from, say, a database, you’re not going to want to perform this overhead each time. This is why we checked for a PostBack (which is what you should be doing anyway when binding unchanging data) so that we will only have to bind once and not each time. With that said, adding the items, or performing data binding, after the controls has been added will add it to the ViewState.

So, that’s pretty much it. If you were to run this code by loading it onto your page, you’ll see the three controls laid out side-by-side. If you change the DropDownList selection, type some text into the TextBox, and click “Update!”, you’ll notice that the ViewState is maintained, and no subsequent trips to the database or use of other binding methods are required.

Next time I’ll add onto this control by making use of delegates for adding custom event handlers (even with a custom event argument class)!

Damn this allergy!

July 14th, 2009 No comments »

Ugh, I’m too distracted to work or, or, even post! I was going to elaborate on my earlier post and talk about composite controls and using delegates to create custom event handlers but, meh. I may do this later or this weekend.

Even my Claritin (well, the Giant rip-off) has failed me. Here I am, only 6 hours since I took it and my throat is dripping like a leaky faucet.

I am reading a new book though, called SQL Tuning (O’Reilly), that I picked up used from Amazon. So far so good. Geeky, yes, but it really gets into the ground-zero levels of performance tuning queries and pretty much how disk and memory buffers are affected with different types of optimizations. Fun stuff.