Thursday, 1 May 2014

Bind images to radiobuttonlist from dropdown selection

No comments
 public DataSet bind2()
    {
        SqlConnection con = new SqlConnection("s");
        con.Open();
        SqlCommand cmd;
        cmd = new SqlCommand("select * from images where id=@id", con); 
        cmd.Parameters.AddWithValue("@id",DropDownList1.SelectedValue);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet dt=new DataSet();
        da.Fill(dt);
        con.Close();
        return dt;
    }

protected void  DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList d1 = (DropDownList)sender;
        if (DropDownList1.SelectedIndex == 0 || DropDownList1.SelectedIndex == -1)
        {
            RadioButtonList1.Items.Clear();
        }
           
        else
        {
            DataSet ds = bind2();
            if (ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    ListItem l1 = new ListItem();
                    l1.Text = "<img width=\"100\" height=\"100\" runat=\"Server\" src=\"img/" + dr["img_path"].ToString() + "\"alt=" + dr["img"].ToString() + "/>";
                    l1.Value = dr["id_sub"].ToString();
                    RadioButtonList1.Items.Add(l1);
                }
            }
        }


Showing radiobuttonlist selected image in Image

 protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet ds = bind3();
        DataTable dt = ds.Tables[0];
        foreach (DataRow dr in dt.Rows)
        {
            Image1.ImageUrl = "img/" + dr["img_path"];
        }
    }