Karine Bosch’s Blog

On SharePoint

InputFormRadioButton


The InputFormRadioButton control can be used to place different choices on a page that mutally exclude each other. This control is located in the Microsoft.SharePoint.WebControls namespace of the Microsoft.SharePoint.dll.

inputformradiobutton

You can declare  an InputFormRadioButton control on an application page using the following syntax:

<spuc:InputFormRadioButton ID="InputFormRadioButton1" runat="server" LabelText="Option 1" ButtonSpacing="10" />
<spuc:InputFormRadioButton ID="InputFormRadioButton2" runat="server" LabelText="Option 2" ButtonSpacing="10" />

Don’t forget to add the page directive:

<%@ Register TagPrefix="spuc" Namespace="Microsoft.SharePoint.WebControls"
             Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

In code this would look as follows:

InputFormRadioButton radiobutton = new InputFormRadioButton();
radiobutton.ID = "inputFormRadioButton1";
radiobutton.LabelText = "Option 1";

If you use the InputFormRadioButton control as coded above, the options will not be mutually exclusive.

 inputformradiobutton-groupname

To achieve this you have to set the GroupName property. The options that must be mutally exclusive must have the same group name:

<spuc:InputFormRadioButton ID="InputFormRadioButton1" runat="server" GroupName ="GroupA"
      LabelText="Option 1 (of group A)" />
<spuc:InputFormRadioButton ID="InputFormRadioButton2" runat="server" GroupName="GroupA"
      LabelText="Option 2 (of group A)" />
<spuc:InputFormRadioButton ID="InputFormRadioButton3" runat="server" GroupName="GroupB"
      LabelText="Option 1 (of group B)" />
<spuc:InputFormRadioButton ID="InputFormRadioButton4" runat="server" GroupName="GroupB"
      LabelText="Option 2 (of group B)" />
<spuc:InputFormRadioButton ID="InputFormRadioButton5" runat="server" GroupName="GroupB"
      LabelText="Option 3 (of group B)" />

But in fact this is not different then in ASP.NET. So a good SharePoint developer is a good ASP.NET developer (while the reverse is not always true 😉 )

The InputFormRadioButton inherits from the ASP.NET RadioButton, located in the System.Web.UI.WebControls namespace of the System.Web.dll and has only 2 additional properties: LabelText and ButtonSpacing.

  • AutoPostBack: set this property to true if you want the page to post back when this option is checked or unchecked. In that case implement an event handler for the CheckedChanged event. 
  • ButtonSpacing: specify the number of pixels you want to be added between the check box and the following control.
  • Checked: set this property to true if the radio button must appear selected.
  • Text: you can set the text property to appear next to the check box, but in that case you also have to set the width property. If you want a label to appear next to the check box, you can better use the LabelText property.
  • LabelText: specify the text that you want to appear next to the check box.
  • GroupName: this property must have the same value for all options that must be mutually exclusive.
Advertisements

7 Comments »

  1. This is very up-to-date info. I think I’ll share it on Digg.

    Comment by Ex Boyfriend | April 9, 2009 | Reply

  2. Hey, nice tips. Perhaps I’ll buy a glass of beer to that man from that chat who told me to go to your blog 🙂

    Comment by Heartburn Home Remedy | April 15, 2009 | Reply

  3. Do you have to loop over the radiobuttons to retrieve the selected value?

    Comment by Wes | May 12, 2009 | Reply

    • Wes, you have 2 possibilities:
      1. On submit in your code behind you loop through the radio button controls and check which one has its Checked property set to true.
      2. You add an event handler to the CheckedChanged event of the InputFormRadioButton control. You can set this in combination with the AutoPostBack property. When set to true you can handle the selected InputFormRadioButton immediately (or set a variable to a specific value that can then be handled on submit of the form). When set to false, the event will fire when the form submits and you can take appropriate action in the event handler:

      InputFormRadioButton rb = new InputFormRadioButton();
      rb.AutoPostBack = true;
      rb.CheckedChanged += new EventHandler(rb_CheckedChanged);

      void rb_CheckedChanged(object sender, EventArgs e)
      {
      // Handle the event here
      }

      Karine

      Comment by Karine Bosch | May 13, 2009 | Reply

  4. Very helpful set of docs on in the InputForm classes. They are sparsely documented in the SDK, this is the most detailed overview of these classes provided anywhere. I should know I have been googling on these classes for more hours then I want to admit. 🙂

    Comment by Mike Knowles | July 9, 2009 | Reply

  5. How Can I display radio button side-by-side rather than row wise?

    For Example I have option Yes and No, I want to display in 1 row?

    Comment by Disha | September 10, 2009 | Reply

  6. Hi Disha,
    If you want to display radio buttons side by side, you will have to use the standard ASP.NET RadioButtonList which has an Orientation attribute that can be set to Horizontal or Vertical. SharePoint doesn’t have a similar control.
    Karine

    Comment by Karine Bosch | September 11, 2009 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: