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

How to add Html Code in Blogspot

Html Code In Blogspot

Introduction : In this article i will show you how you can add html code in Blogger .sometimes we require to display html code in blog too. To add Html code you need to follow just some steps then html code will be able to display in blog .

Open http://centricle.com/tools/html-entities/ Site 
  1. Paste Html Code in box
  2. Then Click on Encode
  3. Copy Encode Code and paste in your blog


Related Other posts

CheckBoxList example In Asp.net c#


CheckBoxList example In Asp.net c# 

Many time we require to bind the data in checkboxlist using database sql server in asp.net . I have here provided complete Html code and Code behind code for binding CheckBoxList In Asp.net c# .Following are my simple code .

Screen Shots :-

Html Code :-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Checkboxlist.aspx.cs" Inherits="Checkboxlist" %>
<!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">
    <table>
        <tr> <td> <h2>  CheckBoxList example</h2> </td> </tr>
        <tr>
            <td> <asp:CheckBoxList ID="chklisttest" runat="server"> </asp:CheckBoxList>  </td>
        </tr>
    </table>
    </form>
</body>
</html>

Code Behind :-

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 Checkboxlist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindCheckBoxList();
        }
    }
    private void bindCheckBoxList()
    {   try
        {
            DataSet Ds = GetDataSet("Select * from Countrytable");
            chklisttest.DataSource = Ds;
            chklisttest.DataTextField = "CountryName";
            chklisttest.DataValueField = "CountryID";
            chklisttest.DataBind();
        }
        catch (Exception){}
    }
    private DataSet GetDataSet(string Query)
    {   DataSet Ds = new DataSet();
        try
        {   string strCon = @"Data Source=Servername;Initial Catalog=Test;Integrated Security=True";
            SqlConnection Con = new SqlConnection(strCon);
            SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
            Da.Fill(Ds);           
        }
        catch (Exception){}
        return Ds;
    }
}


Related Other posts