Thursday, 30 May 2013

How to use RadioButtonList control and do transaction on RadioButtonList using asp.net c# code



How to use RadioButtonList control and do transaction on RadioButtonList using asp.net c# code



Step 1 : First create your website in asp.net and create one .aspx page .

Step2: Open .aspx page and add RadioButtonList control.

<asp:RadioButtonList ID="rbList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="rbList_SelectedIndexChanged"></asp:RadioButtonList>

Step 3 : And add buttons control to do transaction on RadioButtonList.

 <asp:Button ID="BtnAdd" runat="server" OnClick="BtnAdd_Click" Text="Add Item" />
<asp:Button ID="BtnDelete" runat="server" Text="Delete Selcted" OnClick="BtnDelete_Click" />
                      

Step 4 :Add server side code to handle button  event.

 protected void BtnAdd_Click(object sender, EventArgs e)
    {
        ListItem litem = new ListItem();
        itemAdded++;
        litem.Text = "Item" + itemAdded;
        litem.Value = "Item" + itemAdded;
        rbList.Items.Add(litem);
        lblMessage.Text = "Item addedd = " + litem.Value.ToString();
    }

    protected void BtnDelete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rbList.Items.Count; i++)
        {
            if (rbList.Items[i].Selected == true)
            {
                lblMessage.Text = "Item deleted = " + rbList.Items[i].ToString();
                rbList.Items.RemoveAt(i);              
                break;
            }
        }
    }
    protected void rbList_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblMessage.Text = "Selected value = " + rbList.SelectedValue.ToString();
    }

Step 5 : Just run your page and try to check your code.

//-----------------------  Page Design Code -----------------

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <table class="auto-style1">
                <tr>
                    <td>
                        <asp:RadioButtonList ID="rbList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="rbList_SelectedIndexChanged"></asp:RadioButtonList>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="BtnAdd" runat="server" OnClick="BtnAdd_Click" Text="Add Item" />
                        &nbsp;<asp:Button ID="BtnDelete" runat="server" Text="Delete Selcted" OnClick="BtnDelete_Click" />
                        &nbsp;&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lblMessage" runat="server"></asp:Label>
                    </td>
                </tr>
            </table>

        </div>
    </form>
</body>
</html>


//-----------------------  Page Server Side Code -----------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class RadioButtonListContol : System.Web.UI.Page
{
    private static int itemAdded = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        ListItem litem = new ListItem();
        itemAdded++;
        litem.Text = "Item" + itemAdded;
        litem.Value = "Item" + itemAdded;
        rbList.Items.Add(litem);
        lblMessage.Text = "Item addedd = " + litem.Value.ToString();
    }

    protected void BtnDelete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rbList.Items.Count; i++)
        {
            if (rbList.Items[i].Selected == true)
            {
                lblMessage.Text = "Item deleted = " + rbList.Items[i].ToString();
                rbList.Items.RemoveAt(i);              
                break;
            }
        }
    }
    protected void rbList_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblMessage.Text = "Selected value = " + rbList.SelectedValue.ToString();
    }
}

No comments:

Post a Comment

Thank you for your interest .