ASP.NET

An Example of Data Binding with RadioButtonList Control

This is my third post on Data Binding in ASP.NET. Earlier I have explained the concept of Data Binding using examples on two of the available List controls in ASP.NET – DropDownList, and BulletedList. In this article, I will give An Example of Data Binding with RadioButtonList Control.

Besides, explaining data binding, I will also discuss how to use an in-memory database as a data source. Basically, an in-memory database is created using the classes found in the System.Data namespace. The following is a list of classes that we use for this purpose.

  • DataSet
  • DataTable
  • DataRow
  • DataColumn

Create a Database Using DataSet

Even ADO.NET has a provision for creating a database entirely with code using the DataSet class.

DataSet Class

Basically, a DataSet object serves as an in-memory cache of data that can be generated entirely by the code.

DataTable Class

In order to add one or more tables to a DataSet object, we use the DataTable class. A DataTable object represents a table containing rows of data for various fields or columns. Like a relational database table, we first need to create the schema of the table. For this purpose, we can add various objects of the DataColumn class and also specify specific constraints such as the PrimaryKey constraint.

DataColumn Class

For the purpose of creating the schema of the table, we create the objects of the DataColumn class and add them to the corresponding DataTable object. It has several properties like ColumnName, DataType, Unique, AutoIncrement, Readonly, and so on that we can use to create a data column.

DataRow Class

Another component of the DataTable is an object of the DataRow class that represents a single row in the table. In fact, it is the DataRow object only that we use to add, update, and delete data in the Data Table. The DataRow class has a method called NewRow() that adds a new blank row in the data table. Further, we can use the Add() method to add a particular DataRow object.

An Example of Data Binding with RadioButtonList Control

The following example shows how to create DataSet and bind it to the RadioButtonList control. Therefore, we add a RadioButtonList control from the ToolBox and set its properties.

WebForm1.aspx

<body>
    <form id="form1" runat="server">
        <div>
            Select a Course from Available Courses:<br />
            <asp:RadioButtonList ID="RadioButtonList1" 
                 AutoPostBack="true" RepeatColumns="3" RepeatLayout="Table"
                 BackColor="LightYellow" BorderColor="DarkOliveGreen" BorderWidth="3"
                 ForeColor="DarkSalmon" 
                 runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"></asp:RadioButtonList><br /><br />
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        </div>
    </form>
</body>

As shown below, in the Code Behind, we create a DataSet containing a table with the name Courses. Further, we add two DataColumn objects with names Course_Id and CourseName respectively, and assign values to their properties. It creates the schema for the table. Next, we add few DataRow objects to the table.

Finally, bind this dataset to the RadioButtonList control in the BuildList() method. Also, define the SelectedIndexChanged event handler in order to display the values from the selection.

WebForm1.aspx.cs

using System;
using System.Data;

namespace ADOExample2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        DataSet ds;

        public void BuildDataSet()
        {
            ds = new DataSet();
            DataTable dt = new DataTable("Courses");
            DataColumn dc = new DataColumn();
            dc.ColumnName = "Course_Id";
            dc.DataType = Type.GetType("System.Int32");
            dc.Unique = true;
            dt.Columns.Add(dc);

            dt.PrimaryKey = new DataColumn[] { dc };
            dc = new DataColumn();
            dc.ColumnName = "CourseName";
            dc.DataType = Type.GetType("System.String");
            dc.Unique = true;
            dt.Columns.Add(dc);


            DataRow dr = dt.NewRow();
            dr[0] = 1;
            dr[1] = "MCA";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = 2;
            dr[1] = "MBA";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = 3;
            dr[1] = "BCA";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = 4;
            dr[1] = "BBA";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = 5;
            dr[1] = "BCOM";
            dt.Rows.Add(dr);
            ds.Tables.Add(dt);
        }
        public void BuildList()
        {
            BuildDataSet();
            RadioButtonList1.DataSource = ds;
            RadioButtonList1.DataTextField = "CourseName";
            RadioButtonList1.DataValueField = "Course_Id";
            RadioButtonList1.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BuildList();
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int x = Int32.Parse(RadioButtonList1.SelectedItem.Value);
            String str = RadioButtonList1.SelectedItem.Text;
            String str1 = $"You have selected: Course ID: {x}, Course Name: {str}";
            Label1.Text = str1;
        }
    }
}

Output

An Example of Data Binding with RadioButtonList Control
An Example of Data Binding with RadioButtonList Control

Summary

To sum up, it is also possible to create an in-memory dataset instead of using data from a physical database. As I have shown in the example, the dataset is created using the code only. Hence, the schema has been created using the DataColumn object and then we added certain rows. Lastly, we specify the DataSource property of the list control and called the DataBind() method.


Related Topics

A Brief Overview of Laravel

Examples of AdRotator Control in ASP.NET

Overview of MVC architecture in ASP.NET

Binding Data to Web Control in ADO.NET

Examples of Validation Controls in ASP.NET

Data Binding Using BulletedList Control

Leave a Reply

Your email address will not be published. Required fields are marked *