# Sunday, 06 August 2006

Because developing with the .NET framework gives you the freedom to use multiple languages it can be a bit of a hassle to choose between them. VB.NET and C# are probably the most wide spread languages and a question that gets asked regularly on the ASP.NET forums is which one to choose. My answer: learn one of them and when you're familiar with one learn the other. My former company used VB.NET as their default language. In my free time I learned C# though so I became familiar with both. The hardest part isn't actually learning another "dialect" language but learning which classes and namespaces there are available to you is.

Well in aid of those who like to know both languages I found a couple of comparison cheat sheets:

And of course some conversion tools:

Grz, Kris.

kick it on DotNetKicks.com

Sunday, 06 August 2006 14:55:20 (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [1]  | 
# Sunday, 25 June 2006

Most controls are just drag & dropped on a webform or handcoded by a developer in the source view of the document. Although these controls can display dynamic data, for example coming from a database, the controls on the webform themselves remain static. In some scenario's however you want dynamically added controls. For example if a certain authenticated user views the page you want to show more information, or depending on a certain choice you want to load a Repeater control with data or a DateTime control. In this article I instantiate a RadioButtonList control, add it to a PlaceHolder control and wire up the SelectedIndexChanged event of the RadioButtonList instance. Listing 1 shows the C# version and Listing 2 the VB.NET version. Either version is equivalent and it's quite easy to compare both of the code snippets since both have the same equivalent code on the same line.

Listing 1: C# version:

    1 <%@ Page Language="C#" %>

    2 

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

    4 

    5 <script runat="server">

    6     protected override void OnInit(EventArgs e)

    7     {

    8         base.OnInit(e);

    9 

   10         // Instantiate a RadioButtonList control

   11         RadioButtonList rbl = new RadioButtonList();

   12 

   13         rbl.AutoPostBack = true;

   14         rbl.ID = "rblID";

   15 

   16         // Wire up the eventhandler

   17         rbl.SelectedIndexChanged += new EventHandler(rbl_SelectedIndexChanged);

   18 

   19         // Add the items

   20         rbl.Items.Add(new ListItem("One", "1"));

   21         rbl.Items.Add(new ListItem("Two", "2"));

   22         rbl.Items.Add(new ListItem("Three", "3"));

   23 

   24         rbl.DataBind();

   25 

   26         PlaceHolder1.Controls.Add(rbl);

   27     }

   28 

   29     void rbl_SelectedIndexChanged(object sender, EventArgs e)

   30     {

   31         // Get the control and cast it to the

   32         // appropriate type. In our case a RadioButtonList.

   33         RadioButtonList c = (RadioButtonList)FindControl("rblID");

   34         Label1.Text = c.SelectedItem.Text;

   35     }

   36 </script>

   37 

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

   39 <head runat="server">

   40     <title>Untitled Page</title>

   41 </head>

   42 <body>

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

   44     <div>

   45         <asp:PlaceHolder ID="PlaceHolder1" runat="server" /><br />

   46         <asp:Label runat="server" ID="Label1" />

   47     </div>

   48     </form>

   49 </body>

   50 </html>

Listing 2: VB.NET version:

    1 <%@ Page Language="VB" %>

    2 

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

    4 

    5 <script runat="server">

    6 

    7     Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)

    8 

    9 

   10         ' Instantiate a RadioButtonList control

   11         Dim rbl As New RadioButtonList

   12 

   13         rbl.AutoPostBack = True

   14         rbl.ID = "rblID"

   15 

   16         ' Wire up the event handler

   17         AddHandler rbl.SelectedIndexChanged, AddressOf rbl_SelectedIndexChanged

   18 

   19         ' Add the items

   20         rbl.Items.Add(New ListItem("One", "1"))

   21         rbl.Items.Add(New ListItem("Two", "2"))

   22         rbl.Items.Add(New ListItem("Three", "3"))

   23 

   24         rbl.DataBind()

   25 

   26         PlaceHolder1.Controls.Add(rbl)

   27 

   28     End Sub

   29 

   30     Protected Sub rbl_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

   31 

   32         ' Get the control and cast it to the

   33         ' appropriate type. In our case a RadioButtonList.

   34         Dim c As RadioButtonList = DirectCast(FindControl("rblID"), RadioButtonList)

   35         Label1.Text = c.SelectedItem.Text

   36 

   37     End Sub

   38 

   39 </script>

   40 

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

   42 <head runat="server">

   43     <title>Untitled Page</title>

   44 </head>

   45 <body>

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

   47         <div>

   48             <asp:PlaceHolder ID="PlaceHolder1" runat="server" /><br />

   49             <asp:Label runat="server" ID="Label1" />

   50         </div>

   51     </form>

   52 </body>

   53 </html>

On line 11 the RadioButtonList is instantiated. After that it's given an ID and the AutoPostBack property is set to true (line numbers 13 & 14).
Then we wire up the event that'll be handled when a certain selection is made, remember we set the AutoPostBack property to true so once another choice than the current is made an automatic postback to the server will occur. Note the different syntax for C# and VB.NET to wire event handlers on line 17.

At this moment we already have a RadioButtonList instance, wired up the SelectedIndexChanged event, added 3 items to it and databound these items (Lines 20 to 24) but at this moment it's still not visible to the enduser since it was not already put on the page. ASP.NET provides a dedicated control for this: the PlaceHolder control. You can place this control somewhere on your webform and later on use it to hang dynamic controls to. Another convenient control for this is the Panel control. Adding our RadioButtonList to the PlaceHolder control is done on line 26.

We can now render our webform in a browser and upon checking a radiobutton in the list an automatic postback occurs, goes through the code of adding the RadioButtonList again to the page in the Page_Init/OnInit event again, which is necessary because the page has completely forgotten about the existance of it after the previous rendering. After that the rbl_SelectedIndexChanged event gets handled.
In this event we first need to obtain the correct control based upon its ID property with the FindControl method. This method returns an instance of a Control class, which is the base class of all controls in ASP.NET. Once obtained we need to cast it back to the proper class, which is in this case a RadioButtonList. Once cast the Text property of the SelectedItem is used to fill up the Text property of the Label control.

Grz, Kris.

Sunday, 25 June 2006 14:13:21 (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  |