Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Problem in selecting Radio Button inside Repeater Control

    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 3.53k
    Comment on it

    Problem in selecting Radio Button inside Repeater Control:

        Incase we are using RadioButton inside a Repeater, one common problem which we face is that we are not able to select single RadioButton, i.e. If we select one RadioButton, and then click on the other the other also gets selected without unselecting the first one.
      Even the GroupName property of the RadioButton dosen't works for this.

    The solution I got is:

    1. Set the AutoPostBack property of the RadioButton to "true".
    2. Make OnCheckedChanged event for the RadioButton.

    Example:

    <asp:RadioButton ID="rbtnSelectEmp" runat="server" AutoPostBack="true" 
    
    OnCheckedChanged="rbtnSelectEmp_OnCheckedChanged" />
    

    3. Inside OnCheckedChanged event write the following code:

    protected void rbtnSelectEmp_OnCheckedChanged(object sender, EventArgs e)
            {
                foreach (RepeaterItem item in rptEmployee.Items)
                {
                    RadioButton rbtn = (RadioButton)item.FindControl("rbtnSelectEmp");
                    rbtn.Checked = false;
                }
                ((RadioButton)sender).Checked = true;
            }
    

    Understanding the code:
        In foreach loop we are iterating through each item in the Repeater and unchecking each RadioButton inside the loop. At last we set the checked property of the Sender RadioButton to true.(The sender RadioButton is the one on which we have clicked).
      This will set the clicked RadioButton Checked while rest will get Unchecked.
      One important thing that has to be taken care of is that the Repeater Bind function in the Page_Load should be kept inside the !IsPostBack, else no RadioButton will get selected.

 2 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: