Follow us

Header Ads

Search Data By Radio Button in Asp.Net WebForm

 SearchDataRadio.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            width: 106px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table class="auto-style1">
                <tr>
                    <td class="auto-style2">SEARCH</td>
                    <td>
                        <!-- Auto PostBack Property can use when we can not use Search Button  -->
                        <asp:RadioButton ID="MALERadioButton" runat="server" AutoPostBack="True" GroupName="Gender" OnCheckedChanged="MALERadioButton_CheckedChanged" Text="Male" />
&nbsp;
                        <asp:RadioButton ID="FEMALERadioButton" runat="server" AutoPostBack="True" GroupName="Gender" OnCheckedChanged="FEMALERadioButton_CheckedChanged" Text="Female" />&nbsp;

                        <asp:RadioButton ID="BOTHRadioButton" runat="server" AutoPostBack="True" GroupName="Gender" OnCheckedChanged="BOTHRadioButton_CheckedChanged" Text="Both" />
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">&nbsp;</td>
                    <td>
                        <asp:Button ID="SEARCHButton" runat="server" OnClick="SEARCHButton_Click" Text="SEARCH" />
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">&nbsp;</td>
                    <td>
                        <asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black">
                            <FooterStyle BackColor="#CCCCCC" />
                            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
                            <RowStyle BackColor="White" />
                            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                            <SortedAscendingCellStyle BackColor="#F1F1F1" />
                            <SortedAscendingHeaderStyle BackColor="#808080" />
                            <SortedDescendingCellStyle BackColor="#CAC9C9" />
                            <SortedDescendingHeaderStyle BackColor="#383838" />
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

 SearchDataRadio.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System .Configuration;
using System.Data;

public partial class SearchDataRadio : System.Web.UI.Page
{
    string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
      if(!IsPostBack)
        {
            bindGridView();
        }
    }
    void bindGridView()
    {
        SqlConnection con = new SqlConnection(cs);
        string query = "select * from GridRadio";
        SqlDataAdapter sda = new SqlDataAdapter(query, con);
        DataTable data = new DataTable();
        sda.Fill(data);
        GridView1.DataSource = data;
        GridView1.DataBind();
    }
    void SearchDataByMale()
    {
        SqlConnection con = new SqlConnection(cs);
        string query = "select * from GridRadio where gender=@male";
        SqlDataAdapter sda = new SqlDataAdapter(query, con);
        sda.SelectCommand.Parameters.AddWithValue("@male", MALERadioButton.Text);
        DataTable data = new DataTable();
        sda.Fill(data);
        GridView1.DataSource = data;
        GridView1.DataBind();
    }
    void SearchDataByFemale()
    {
        SqlConnection con = new SqlConnection(cs);
        string query = "select * from GridRadio where gender=@female";
        SqlDataAdapter sda = new SqlDataAdapter(query, con);
        sda.SelectCommand.Parameters.AddWithValue("@female", FEMALERadioButton.Text);
        DataTable data = new DataTable();
        sda.Fill(data);
        GridView1.DataSource = data;
        GridView1.DataBind();
    }

    protected void SEARCHButton_Click(object sender, EventArgs e)
    {
        if(MALERadioButton.Checked==true)
        {
            SearchDataByMale();
        }
        else if(FEMALERadioButton.Checked==true)
        {
            SearchDataByFemale();
        }
        else if(BOTHRadioButton.Checked==true)
        {
            bindGridView();
        }
        else
        {
            Response.Write("<script>('Pleae select any gender !!')</script>");
        }
    }
    //This event automatic search data without using Search button.
    protected void MALERadioButton_CheckedChanged(object sender, EventArgs e)
    {
        SearchDataByMale();
    }
    //This event automatic search data without using Search button.
    protected void FEMALERadioButton_CheckedChanged(object sender, EventArgs e)
    {
        SearchDataByFemale();
    }
    //This event automatic search data without using Search button.
    protected void BOTHRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        bindGridView();
    }
}

Post a Comment

0 Comments