RadioButton checkedchanged event firing multiple times

Posted by kash3 on Stack Overflow See other posts from Stack Overflow or by kash3
Published on 2010-04-23T04:04:32Z Indexed on 2010/04/23 4:13 UTC
Read the original article Hit count: 263

Filed under:

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:

<asp:GridView ID="GridView1" runat="server" 
        AutoGenerateColumns="False" BackColor="White"
        BorderColor="#CC9966" BorderStyle="None" EnableViewState="true"   
        BorderWidth="1px" CellPadding="4" Font-Names="Verdana">
  <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
  <Columns>     
      <asp:TemplateField HeaderText="Select One">
        <ItemTemplate>         
        </ItemTemplate>
      </asp:TemplateField>      
       <asp:TemplateField HeaderText="Select Two">
        <ItemTemplate>         
        </ItemTemplate>
      </asp:TemplateField>
       <asp:TemplateField>
                <ItemTemplate>
                <asp:Label ID="lblval" runat="server" Text="!" ForeColor="Red" Visible="false"/>
                </ItemTemplate>
            </asp:TemplateField>
  </Columns>

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

© Stack Overflow or respective owner

Related posts about radiobuttons