April 19, 2008
@ 08:07 PM

 

I admit it - the above comic could be about me. I can be easily distracted by shiny things, so I sometimes have to fight off the urge to spend way too much time playing around with some cool bit of technology, code, or idea. If you're like me, these links may be your downfall. Lot's of things to sidetrack you with. The site I snagged the above comic from is just one of them.

http://www.xkcd.com

MIX is a web development conference put on by Microsoft. They've made all (well, at least I think it's all of them) of the conference topics available online. Very cool - there are bunch of really good sessions available here. Silverlight is looking interesting.

http://visitmix.com/

The TED (Technology, Entertainment, Design) conference is a four day conference with one "track" of speakers that each get 18 minutes to there thing. From what I've gathered, it's mostly an "invite only" type of event of around 1000 people (and even if it wasn't, the $6000 membership fee would probably keep the number of people attending under control). At any rate, they've made something like 200 of the talks available for free, so there are a ton of interesting videos to watch here. Since they're all under 18 minutes, it's easy to fit a video or two in a sitting. Then you can waste another few hours Googling some of the things they talk about.

http://www.ted.com

Google Earth - An "oldie" but a goodie. I can waste a ton of time with this one. Something about the interactivity of it really appeals to me. I can't say I was ever really a map-person until apps. like this and GPS became readily availble. And if you get bored, there are a bunch of neat 3D buildings and map overlays to download. Zoom in, zoom out, zoom in, zoom out...

http://earth.google.com/

Photosynth - If Google Earth appeals to you, you'll probably also love this one. It's basically a Microsoft research project (well, Microsoft owns it now) that takes pictures of some scene and projects it (or builds it, I'm not exactly sure) onto a 3d framework of the original location. Check out the Collections they've got - I swear I wasted a good 45 minutes rotating and zooming into the images. Even if you've seen this in the past, visit it again: they've added a few new collections that are pretty cool.

http://labs.live.com/photosynth/

The above should keep you busy for a while. I had planned on "featuring" a timewaster once a week or so, but at my current blogging pace it'd probably be closer to once a month, so I decided to just group a bunch of the more general ones together into one post. Have fun.


 
Categories: Other

April 15, 2008
@ 10:18 PM

One request that comes up from time to time is to embed a checkbox inside of an ASP.NET grid control for boolean data.

Unfortunately, one of the downsides to this is that the controls are "read only" - they don't let you check or uncheck them

without putting the row into edit mode. That works, but in some cases this isn't all that great either. What do you do if you just want a checkbox on each row, that's "live" and causes a postback as you check/uncheck items? Thankfully this doesn't take much code but figuring out the first time can be really painful. To get the "live" checkbox, just wrap it inside of a template. (In the example code I created a new Web Form named "GridCheckbox").

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridCheckbox.aspx.cs" Inherits="WebApplication1.GridCheckbox" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>       

        <asp:GridView runat="server" ID="grdList" AutoGenerateColumns="false"

            onrowdatabound="grdList_RowDataBound">

            <Columns>

                <asp:TemplateField>

                    <ItemTemplate>                       

                        <asp:CheckBox runat="server" ID="chkItem" Checked='<%# Container.DataItem %>' AutoPostBack="True" OnCheckedChanged="CheckChanged"/>                                              

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>

    </div>

    </form>

</body>

</html>

 

Make sure you set the "AutoPostBack" to true. Then set it's OnCheckedChanged event handler to call your server side code.

Now let's add some code to the Page_Load to give our grid something to bind to:

protected void Page_Load(object sender, EventArgs e)

{

    bool[] items = { true, true, false, false, true };

    if (!Page.IsPostBack)

    {

        this.grdList.DataSource = items;

        this.grdList.DataBind();

    }

}

 

 

In my example code, I'm just binding to a simple boolean collection to keep things simple. Now we need an event handler to respond to our checkbox event:

protected void CheckChanged(object sender, EventArgs e)

{          

}

 

The code doesn't do anything yet but at this point you might be wondering how we figure out which checkbox we checked - normally, your grid is bound to a different row in some table. Each checkbox is related to that specific row. How or where do we get that from? The grid isn't much help in this case because it's not raising the event. If we were using a Button we could set it's CommandName and/or CommandArgument properties to hold something like the primary key of the row in our table. The checkbox control doesn't have any such properties. ASP.NET controls have an InputAttributes collection where you can place name/value pairs - this information is passed to the client (and back to the server), so it makes this relatively painless.

First, add an event to onRowDataBound of the grid (already shown in the ASPX above). Now add the event in our code-behind:

protected void grdList_RowDataBound(object sender, GridViewRowEventArgs e)

{       

}

 

We can't reference the control directly by name, since it was embedded in the grid and ASP.NET has made sure it's uniquely named. Let's use the FindControl method to do this. I've also added the code to add a Value attribute and added a member called m_row. I'm just using this so we can have a different value on each and every row of the grid. In a real application this is where you'd pull the primary key value from your datasource (probably using e.Row.DataItem).

private int m_row = 0;

protected void grdList_RowDataBound(object sender, GridViewRowEventArgs e)

{

    CheckBox chk = e.Row.FindControl("chkItem") as CheckBox;

 

    if (chk != null)

        chk.InputAttributes.Add("Value", this.m_row.ToString());

    this.m_row++;

}


Now we're just about done - all that's left is retrieving this value back on the server when the Checkbox is checked or unchecked:

protected void CheckChanged(object sender, EventArgs e)

{

    CheckBox chk = sender as CheckBox;

    string val = "";

    if (chk != null)

        val = chk.InputAttributes["Value"];

}


So our completed codebehind page looks like this:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

 

namespace WebApplication1

{

    public partial class GridCheckbox : System.Web.UI.Page

    {

        private int m_row = 0;

        protected void Page_Load(object sender, EventArgs e)

        {

            bool[] items = { true, true, false, false, true };

            if (!Page.IsPostBack)

            {

                this.grdList.DataSource = items;

                this.grdList.DataBind();

            }

        }

 

        protected void CheckChanged(object sender, EventArgs e)

        {

            CheckBox chk = sender as CheckBox;

            string val = "";

            if (chk != null)

                val = chk.InputAttributes["Value"];

        }

 

        protected void grdList_RowDataBound(object sender, GridViewRowEventArgs e)

        {

            CheckBox chk = e.Row.FindControl("chkItem") as CheckBox;

 

            if (chk != null)

                chk.InputAttributes.Add("Value", this.m_row.ToString());

            this.m_row++;

        }

    }

}

Now when you run this every time you check or uncheck an item you should see the page post-back (set a breakpoint on CheckChanged). You can now update your data, or whatever else you need to do in response to this event.
 
Categories: ASP.NET

April 4, 2008
@ 06:15 PM

This has hit me more than once already. If you create an HTTP Handler (ASHX) file, by default the context object reference (passed in to ProcessRequest) doesn't contain a reference to the Session object, so attempting to access context.Session will fail.

The fix is to add a "using System.Web.SessionState" to the using section, then inherit from the IReadOnlySessionState interface.

 

Ex.

 

using System.Web.SessionState;

public class MyAshx : IHttpHandler, IReadOnlySessionState
{

}

 

This will give you read only access to the session state. If you need to add to the session, inherit from IRequiresSessionState instead.


 
Categories: ASP.NET

April 3, 2008
@ 08:19 PM

I've been playing around a bit with WPF lately (with Visual Studio 2008) and was disappointed that there wasn't any intellisense in the designer (like what you'd see in the ASP.NET designer). I happened to mention this on the UT and John Fenton there pointed me to this link:

 

http://blogs.msdn.com/windowssdk/archive/2008/02/22/workaround-installing-win-sdk-after-vs2008-breaks-xaml-intellisense.aspx

 

Apparently installing the Windows SDK after installing VS2008 breaks XAML intellisense. The link above walks through fixing it (very simple registry entry fix).

 

Links

 

http://www.universalthread.com
http://blogs.msdn.com/windowssdk/archive/2008/02/22/workaround-installing-win-sdk-after-vs2008-breaks-xaml-intellisense.aspx


 
Categories: Visual Studio | WPF