Search OR Filter Grid View By Using Radio Buttons In ASP.NET Web Forms

 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;


namespace SEARCH_DATA_RADIO

{

    public partial class WebForm1 : 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 tbl";

            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 tbl 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 tbl 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>alert('Please select any gender !!')</script>");

            }

        }


        protected void MALERadioButton_CheckedChanged(object sender, EventArgs e)

        {

            SearchDataByMale();

        }


        protected void FEMALERadioButton_CheckedChanged(object sender, EventArgs e)

        {

            SearchDataByFemale();

        }


        protected void BOTHRadioButton_CheckedChanged(object sender, EventArgs e)

        {

            bindGridView();

        }

    }

}

Above File is SEARCH_DATA_RADIO\WebForm1.aspx.cs






<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SEARCH_DATA_RADIO.WebForm1" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <style type="text/css">

        .auto-style1 {

            width: 400px;

        }

        .auto-style2 {

            width: 98px;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <table cellpadding="3" cellspacing="3" class="auto-style1">

                <tr>

                    <td class="auto-style2">SEARCH</td>

                    <td>

                        <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" Height="38px" OnClick="SEARCHButton_Click" Text="SEARCH" Width="83px" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2">

                        <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>

Above File is SEARCH_DATA_RADIO\WebForm1.aspx













Comments

Popular posts from this blog

AJAX Method Of jQuery AJAX In ASP.NET Web Forms | Learn ASP.NET Web Forms

List Box In ASP.NET Web Forms | ASP.NET ListBox | Web Forms Tutorial | ASP.NET

Display Auto Increment Value In Text Field After Insert To Database ASP.NET