How to bind dropdown or call function when i click on any of radio buttons

searchsanjaysharma
searchsanjaysharma used Ask the Experts™
on
How to enable dropdown to bind data when i click on any of the radio button with any postback.
  <asp:RadioButton ID="rbmale" runat="server" Text="M" GroupName="sex"
                    CssClass="report_color" AutoPostBack="True"
                    oncheckedchanged="rbmale_CheckedChanged"/>
                <asp:RadioButton ID="rbfemale" runat="server" Text="F" GroupName="sex"
                    CssClass="report_color" AutoPostBack="True"/>

<asp:dropdownlist id="ddlcountries" runat="server"/>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try below code  

aspx code

<asp:RadioButton ID="rbmale" runat="server" Text="M" GroupName="sex"
                    CssClass="report_color" AutoPostBack="True"
                    oncheckedchanged="rbmale_CheckedChanged"/>
                <asp:RadioButton ID="rbfemale" runat="server" Text="F" GroupName="sex"
                    CssClass="report_color" AutoPostBack="True" oncheckedchanged="rbfemale_CheckedChanged"/>

<asp:dropdownlist id="ddlcountries" runat="server"/>

.cs code

 protected void rbmale_CheckedChanged(object sender, EventArgs e)
{
    FetchData(rbmale.selectedvalue);
}

protected void rbfemale_CheckedChanged(object sender, EventArgs e)
{
        FetchData(rbfemale.selectedvalue);
}

private void FetchData(string value)
{
    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnString"]) // calling up your connection string that was configured  in you Web Config File
        {
            DataTable dt = new DataTable();


                conn.Open();
        String sql = "SELECT * FROM Table2 WHERE gender = '" +value+ "'"
                SqlCommand cmd = new SqlCommand(sql, conn);
               
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();

        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        ad.Fill(dt);
       
       if (dt.Rows.Count > 0)
        {
            DropDownList1.Items.Add("Select");
            for (int j = 0; j< dt.Rows.Count; j++)
                    {
                       
                        Cat = dt.Rows[j]["Category"].ToString();
                        DropDownList1.Items.Add(Cat);
                    }

        }
        cmd.Connection.Close();
               

        }

}

Author

Commented:
I dont want to autopostback , i want it without autopost back.

Author

Commented:
sorry i wrote any postback
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Senior Team Manager
Commented:
how do you want to populate the dropdown postback with page refresh OR postback without page refresh?

the code given by vishal_114 will work with the auto postback (page refresh).

just modify the design code with an update panel to avoid page refresh
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
	<ContentTemplate>

	<asp:RadioButton ID="rbmale" runat="server" Text="M" GroupName="sex"
							CssClass="report_color" AutoPostBack="True"
							oncheckedchanged="rbmale_CheckedChanged"/>
						<asp:RadioButton ID="rbfemale" runat="server" Text="F" GroupName="sex"
							CssClass="report_color" AutoPostBack="True" oncheckedchanged="rbfemale_CheckedChanged"/>

		<asp:dropdownlist id="ddlcountries" runat="server"/>	
		
	</ContentTemplate>
</asp:UpdatePanel>

Open in new window

Author

Commented:
i cant use update panel as the task is in framework 2.0

Author

Commented:
ok

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial