locked
Radio Button RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have 2 Radio Button (Processed , UnProcessed) . I want if Processed is clicked then all those records which have status = 'T' should be displayed . If UnProcessed then those records which have status = 'F' should be displayed in Gridview.  I have below code but it is showing same data in both case i.e Processed

    protected void rbtn_CheckedChanged(object sender, EventArgs e)
            {
                if (rbtnProcessed.Checked)
                    GetData();
                else
                    GetNonData();
            }

    Thanks

    Monday, December 3, 2018 10:18 AM

Answers

  • User283571144 posted

    Hi JagjitSingh,

    According to your description,I suggest you could use radio button’s OnCheckedChanged event to achieve your requirement.

    We could rebind the gridview datasource according to radiobutton value.

    More details, you could refer to below codes:

    ASPX:

    <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
                <asp:RadioButton ID="RadioButton1" runat="server"  Text="T" AutoPostBack="true" OnCheckedChanged="RadioButton1_CheckedChanged"  GroupName="sel"/>
                <asp:RadioButton ID="RadioButton2" runat="server"  Text="F" AutoPostBack="true" OnCheckedChanged="RadioButton2_CheckedChanged"  GroupName="sel"/>
            </div>
        </form>
    
    

    Code-behind:

    public partial class Radiotest : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
               
            }
            //select T
            protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
            {
                string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Data;Integrated Security=True;";
                using (SqlConnection connect = new SqlConnection(con))
                {
                    connect.Open();
                    SqlDataAdapter sda1 = new SqlDataAdapter("select * from date where status='T'",connect);
                    DataSet ds1 = new DataSet();
                    sda1.Fill(ds1);
                    this.GridView1.DataSource = ds1;
                    this.GridView1.DataBind();
                }
            }
            //select F
            protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
            {
                string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Data;Integrated Security=True;";
                using (SqlConnection connect = new SqlConnection(con))
                {
                    connect.Open();
                    SqlDataAdapter sda = new SqlDataAdapter("select * from date where status='F'", connect);
                    DataSet ds= new DataSet();
                    sda.Fill(ds);
                    this.GridView1.DataSource = ds;
                    this.GridView1.DataBind();
                }
    
            }
        }
    

    Result:

    Besides, if you use RadioButtonList, I suggest you could add the check logic inside the OnSelectedIndexChanged event and rebind the gridview.

    More details, you could refer to below codes:

    <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
                <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"  >
                   <asp:ListItem>T</asp:ListItem>
                   <asp:ListItem>F</asp:ListItem>
                </asp:RadioButtonList>
            </div>
        </form>
    
    

    Code-behind:

    public partial class radiobutton : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
            {
               
                string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Data;Integrated Security=True;";
                using (SqlConnection connect = new SqlConnection(con))
                {
                    connect.Open();
                    for (var i = 0; i < RadioButtonList1.Items.Count; i++)
                    {
                        if (RadioButtonList1.SelectedItem.Text=="T")
                        {
                            SqlDataAdapter sda1 = new SqlDataAdapter("select * from date where status='T'",connect);
                            DataSet ds1 = new DataSet();
                            sda1.Fill(ds1);
                            this.GridView1.DataSource = ds1;
                            this.GridView1.DataBind();
                        }
                        if (RadioButtonList1.SelectedItem.Text=="F")
                        {
                            SqlDataAdapter sda2 = new SqlDataAdapter("select * from date where status='F'",connect);
                            DataSet ds2 = new DataSet();
                            sda2.Fill(ds2);
                            this.GridView1.DataSource = ds2;
                            this.GridView1.DataBind();
                        }
                    }
                    
                }
                }
            }
      
    

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 4, 2018 6:08 AM

All replies

  • User283571144 posted

    Hi JagjitSingh,

    According to your description,I suggest you could use radio button’s OnCheckedChanged event to achieve your requirement.

    We could rebind the gridview datasource according to radiobutton value.

    More details, you could refer to below codes:

    ASPX:

    <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
                <asp:RadioButton ID="RadioButton1" runat="server"  Text="T" AutoPostBack="true" OnCheckedChanged="RadioButton1_CheckedChanged"  GroupName="sel"/>
                <asp:RadioButton ID="RadioButton2" runat="server"  Text="F" AutoPostBack="true" OnCheckedChanged="RadioButton2_CheckedChanged"  GroupName="sel"/>
            </div>
        </form>
    
    

    Code-behind:

    public partial class Radiotest : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
               
            }
            //select T
            protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
            {
                string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Data;Integrated Security=True;";
                using (SqlConnection connect = new SqlConnection(con))
                {
                    connect.Open();
                    SqlDataAdapter sda1 = new SqlDataAdapter("select * from date where status='T'",connect);
                    DataSet ds1 = new DataSet();
                    sda1.Fill(ds1);
                    this.GridView1.DataSource = ds1;
                    this.GridView1.DataBind();
                }
            }
            //select F
            protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
            {
                string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Data;Integrated Security=True;";
                using (SqlConnection connect = new SqlConnection(con))
                {
                    connect.Open();
                    SqlDataAdapter sda = new SqlDataAdapter("select * from date where status='F'", connect);
                    DataSet ds= new DataSet();
                    sda.Fill(ds);
                    this.GridView1.DataSource = ds;
                    this.GridView1.DataBind();
                }
    
            }
        }
    

    Result:

    Besides, if you use RadioButtonList, I suggest you could add the check logic inside the OnSelectedIndexChanged event and rebind the gridview.

    More details, you could refer to below codes:

    <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
                <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"  >
                   <asp:ListItem>T</asp:ListItem>
                   <asp:ListItem>F</asp:ListItem>
                </asp:RadioButtonList>
            </div>
        </form>
    
    

    Code-behind:

    public partial class radiobutton : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
            {
               
                string con = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Data;Integrated Security=True;";
                using (SqlConnection connect = new SqlConnection(con))
                {
                    connect.Open();
                    for (var i = 0; i < RadioButtonList1.Items.Count; i++)
                    {
                        if (RadioButtonList1.SelectedItem.Text=="T")
                        {
                            SqlDataAdapter sda1 = new SqlDataAdapter("select * from date where status='T'",connect);
                            DataSet ds1 = new DataSet();
                            sda1.Fill(ds1);
                            this.GridView1.DataSource = ds1;
                            this.GridView1.DataBind();
                        }
                        if (RadioButtonList1.SelectedItem.Text=="F")
                        {
                            SqlDataAdapter sda2 = new SqlDataAdapter("select * from date where status='F'",connect);
                            DataSet ds2 = new DataSet();
                            sda2.Fill(ds2);
                            this.GridView1.DataSource = ds2;
                            this.GridView1.DataBind();
                        }
                    }
                    
                }
                }
            }
      
    

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 4, 2018 6:08 AM
  • User-1499457942 posted

    Hi Brando

      Thanks . How to set RadioButton1 as checked by Default.

    Thanks

    Tuesday, December 4, 2018 7:19 AM