.Net Tip: Default Button for Enter Key

I don’t know if I should be happy to now know about this, or just conserned that it’s taken me this long to discover. But one issue that surfaces time and time again when programming in ASP.NET, is that issue that pressing enter/return in a text feild doesn’t always do what you want it to do. 

On a normal website you can have many forms each with their own submit button which becomes the default action when pressing return on one of the forms feilds. However in ASP.NET Web Forms there is only ever one form on a page, but there could be 10 different buttons each needing to be the default action for a particular text box. 

The solution as it turns out is very simple and you have two options both introduced in .NET 2.0 (yes that’s how old it is!) 

1. Default button for the form. If your page has more than one button, but there is only one that you want to fire when you hit enter then in the code behind you can just type… 

Form.DefaultButton = Button1

or it can also be specified in your aspx file 

<form runat="server" defaultbutton="Button1">

2. If you need to be more specific a panel can also have a default button… 

Panel1.DefaultButton = Button1

or again is can be specified in your aspx file

<asp:Panel runat="server" DefaultButton="Button1">

.NET Charts (Pleasing clients by giving them a graph to look at)

Irruspective of if your working on some kind of company extranet or the admin side of a public facing site, one thing that will make you’re clients go ooooooo and love your work is the inclusion of a funky looking chart. It may not serve any amazing purpose, but as there looking through all the boring text area’s and buttons that actually make up the functinality of the site, the inclusion of nice looking chart is going to make them go “oooo that’s nice” and like you even more. For those of us working in .NET, thank’s to an update from Microsoft at the end of 2008 it’s also something that’ve very quick an easy to do. Better yet the update was free so the only cost is the time you take to impletement it. 

First off if you want to use the chart’s and you haven’t downloaded them then that’s what you need to do. The chart’s shipped after .NET 3.5 so there a seperate install, .NET 4 however has them included by default. 

Using the Chart Control

Like I said adding a chart to a page is a quick and easy thing to do. Once you have the Visual Studio add on installed you can also drag and drop everything into place. However im going to go into a bit more detail. 

To start your going to need a data source. In this example im using a SQL Data Source object for ease of use, in a production environment I heavely recomend against using them as there going to make your code a completly unmanagable mess, instead I would use something like a Entity Data Source. My chart is going to be showing a graph of mobile phone handset popularity so my SQL is simply just returning a table of phone names and how many people use them. 

Code so far: 

<asp:SqlDataSource ID=”ChartDB” runat=”server” ConnectionString=”<%$ ConnectionStrings:ConnectionString %> SelectCommand=”SELECT [Name], [People] FROM [PhonePopularity]”>

Next we need to add a chart, the easiest way to do this is to just drag a chart object onto the page from the toolbox, however if you do want to type it yourself it’s not particularly complex. 

First you will need to register the assembly on the page.. 

 <%@ Register assembly=”System.Web.DataVisualization, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ namespace=”System.Web.UI.DataVisualization.Charting” tagprefix=”asp” %> 

And then add the chart like so…

<asp:Chart ID=”Chart1″ runat=”server” DataSourceID=”ChartDB” Height=”400px” Width=”400px”>

Now we’re ready to start customizing what type of chart we’re going to have and what data it should show from our data source.

To actually show a chart there’s two bits of information you have to describe, area’s and series’. The first area’s is used to define an area for a chart to appear, one interesting thing about the chart control is you arn’t limited to just one area. In fact in this example I’m going to have to chart’s one showing a pie chart of phone popularity that will quickly show what kind of share each phone has, and then a second bar chart making it more clear the actual numbers people have of each phone. Area’s also let you set properties on what the chart is actually going to look like as well. In this instance I’m going to set for both charts to be 3D.

The second bit of information is the Series. This is where you’re actually specifying what data is going to be shown in which area and what kind of chart it is (e.g. Pie, Column, Donut etc). My completed code then looks like this…

<asp:Chart ID=”Chart1″ runat=”server” DataSourceID=”ChartDB” Height=”400px” Width=”400px”>


<asp:Series ChartArea=”ChartArea1″ ChartType=”Pie” Name=”Series1″ XValueMember=”Name” YValueMembers=”People”>
<asp:Series ChartArea=”ChartArea2″ Name=”Series2″ XValueMember=”Name” YValueMembers=”People”>


<asp:ChartArea AlignmentOrientation=”Horizontal” Name=”ChartArea1″>

<Area3DStyle Enable3D=”True” />

<asp:ChartArea Name=”ChartArea2″>

<area3dstyle enable3d=”True” />




Depending on your data this should give you something like this…

This is just a simple example of what you can do, but if you download the Chart Samples Project and have a look through there is no end to the possibilites with everything from different styles of charts to adding ajax functionality even with the ability to click of different parts of the carts to trigger events.