views:

219

answers:

0

Hi, I am trying to add multiple radiobutton columns to my gridview dynamically in the code and i want to implement some logic which involves database fetch in the checkedchanged event of radiobuttons but some how the checked changed event is being fired multiple times for each row.

Following is the code:

aspx:

BorderWidth="1px" CellPadding="4" Font-Names="Verdana">



code behind

void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.DataItem != null) { DataRowView dvRowview = (DataRowView)e.Row.DataItem; int currentRow = GridView1.Rows.Count;

            RadioButton rdoSelect1 = new RadioButton();
            rdoSelect1.GroupName = "Select" + currentRow;
            rdoSelect1.ID = string.Concat("rdoSelect1", currentRow);
            rdoSelect1.AutoPostBack = true;

            rdoSelect1.CheckedChanged += new EventHandler(rdoSelect_CheckedChanged);

            e.Row.Cells[0].Controls.Add(rdoSelect1); 

            RadioButton rdoSelect2 = new RadioButton();
            rdoSelect2.GroupName = "Select" + currentRow;
            rdoSelect2.ID = string.Concat("rdoSelect2", currentRow);
            rdoSelect2.AutoPostBack = true;

            rdoSelect2.CheckedChanged += new EventHandler(rdoSelect_CheckedChanged);

            e.Row.Cells[1].Controls.Add(rdoSelect2);

            if (!IsPostBack)
            {
                e.Row.Cells[e.Row.Cells.Count - 1].Controls[1].Visible = false;

                if (e.Row.Cells[0] != null && Convert.ToBoolean(dvRowview["Select1"]) == true)
                    rdoSelect1.Checked = true;
                else
                    rdoSelect1.Checked = false;

                if (e.Row.Cells[0] != null && Convert.ToBoolean(dvRowview["Select2"]) == true)
                    rdoSelect2.Checked = true;
                else
                    rdoSelect2.Checked = false;
           }
        }  
    }

void rdoSelect_CheckedChanged(object sender, EventArgs e) {
RadioButton rdoSelectedOption = (RadioButton)sender; GridViewRow selRow = rdoSelectedOption.NamingContainer as GridViewRow;

        if (rdoSelectedOption.Checked)   
            selRow.Cells[selRow.Cells.Count - 1].Controls[1].Visible = true;
        else
            selRow.Cells[selRow.Cells.Count - 1].Controls[1].Visible = false;         
    }

i want the checkedchanged event to fire only once for a group name and row.