Pages

Tuesday, December 27, 2011

RadioButtonList Control In Asp.net c#

RadioButtonList Control In Asp.net c# 
Introduction :  In this article i will show you how to bind Radiobuttonlist in asp.net c# .Radiobuttonlist control is group of Radiobutton.Many time we require to bind the data in RadioButtonList using database sql server in asp.net . I have here provided complete Html code and Code behind code for binding RadioButtonList In Asp.net c# .Following are my simple code .

Html Code :-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadioButtonList.aspx.cs" Inherits="RadioButtonList" %>
<!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 runat="server"> <title></title> </head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr> <td> <h2> RadioButtonList example</h2></td> </tr>
                <tr>
                       <td> <asp:RadioButtonList ID="rblRadiobuttonlistTest" CellPadding="1" CellSpacing="1" RepeatDirection="Vertical" runat="server"></asp:RadioButtonList>  </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

Server Side Code :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class RadioButtonList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindRadioButtonList();
        }
    }
    private void bindRadioButtonList()
    {   try{
            DataSet Ds = GetDataSet("Select * from Countrytable");
            rblRadiobuttonlistTest.DataSource = Ds;                 // Set DataSource Here
            rblRadiobuttonlistTest.DataTextField = "CountryName";   // Assign DataText Field
            rblRadiobuttonlistTest.DataValueField = "CountryID";    // Assign Value Field
            rblRadiobuttonlistTest.DataBind();
            }  catch (Exception) { }
    }
    private DataSet GetDataSet(string Query)
    {   DataSet Ds = new DataSet();
        try{
            string strCon = @"Data Source=ServerName\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True";  //Conntection String
            SqlConnection Con = new SqlConnection(strCon);
            SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
            Da.Fill(Ds);
           } catch (Exception) { }
        return Ds; 
    }
}
Conclusion : It is very easy to bind the radiobuttonlist control in asp.net .

Related Other posts

1 comment:

  1. It is helpful for me . good code
    thank you
    amol mandavkar

    ReplyDelete