Saturday 26 July 2014

How to bind data to RadioButtonList control in ASP.NET using C# and vb.net

Radiobuttonlist in asp.net


Description:-

In this example we explain that how to bind RadiobuttonList in Asp.Net or bind data to RadioButtonList from database in asp.net.

radiobuttonlist control enable user to select an item from list of radiobutton. data bind to radibutton is programmatically from database. We can also populate it manually by input list item inside radiobuttonlist tag.

In this example we have list of movies are bind to RadiobuttonList and user can easily select it and the selected value of the radibuttonlist are shown in in messagebox that you have selected this movie.

RadiobuttonList is a group of radio button and are used when there is a multiple item that can be display to user in a list.

Restrict the size of File when Uploaded How to Restrict the size of File when uploaded by user

Dynamically Read/Write File in asp.Net How to Read Write File in Asp.Net using C#



Default2.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>


<!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 id="Head1" runat="server">
    <title>How to use Radiobutton list in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:radiobuttonlist ID="rbl_movies" runat="server"></asp:radiobuttonlist>
    </div>
    <div>
     <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /><br />
     <asp:Label ID="lbl_selectedmovie" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>


Default2.aspx.cs:-

using System;
using System.Data;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Data Source=kiritdemo;Initial Catalog=Demo;Integrated Security=True");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            populate_radiolist();
        }
    }

    //bind data in radiobuttonlist
    private void populate_radiolist()
    {
        DataSet ds = new DataSet();
        string cmdstr = "select id,moviename from movie";
        SqlDataAdapter adp = new SqlDataAdapter(cmdstr, conn);
        adp.Fill(ds);
        rbl_movies.DataSource = ds;
        rbl_movies.DataTextField = "movie";
        rbl_movies.DataValueField = "id";
        rbl_movies.DataBind();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //display selected movie by user
        lbl_selectedmovie.Text = rbl_movies.SelectedItem.ToString();
    }
}



0 comments:

Post a Comment