Avatar of Tom Knowlton
Tom Knowlton
Flag for United States of America

asked on 

How to find-out which RadioButton was selected

I have a web form that has a few user controls on it (.ascx)

One control is a collection of other user controls.

Each of the user controls in the collection have a radio button you can select.  Some javascript code ensures that the radio buttons are mutually exclusive.

When I click a button on the form...I want to find-out which of the user controls in the collection has the radio button selected.


NOTE:  I've noticed that when I click the button and the autopostback fires...the controls inside the control collection are not preserved.  So I need to gather the radio button status before this happens.
//RADIO BUTTON MARKUP (ASCX)
<%@ Control ClassName="CaptchaImageDisplay" Language="C#" AutoEventWireup="true" CodeFile="CaptchaImageDisplay.ascx.cs" Inherits="UserControls_SEO_CaptchaImageDisplay" %>
 
 
<style type="text/css">
    .style1
    {
        width: 70px;
    }
</style>
 
<table width="100">
    <tr>
        <td align="center" class="style1">
        &nbsp;<asp:Image ID="ImageCaptcha" runat="server" BorderStyle="Solid" 
                BorderWidth="2px" Height="100px" Width="100px" />
        </td>
    </tr>
    <tr>
        <td align="center" class="style1">
        &nbsp;<asp:RadioButton ID="RadioButtonCaptchaSelected" runat="server" />
        </td>
    </tr>
</table>
 
//ENFORCES MUTEX FOR THE RADIO BUTTONS
<script type="text/javascript">
        function RadioChecked(current)
        {     
           for(i = 0; i < document.forms[0].elements.length; i++)
           {
              elm = document.forms[0].elements[i]
                                      
              if (elm.type == 'radio')      
              {           
                    if(elm != current)
                    {
                        elm.checked = false;                                      
                    }
              }
           }           
        }
</script>
 
 
 
 
 
 
//RADIO BUTTON CODE-BEHIND (ASCX)
[Serializable]
public partial class UserControls_SEO_CaptchaImageDisplay : System.Web.UI.UserControl
{    
 
    public WebPageImage wpi;
 
    private string _url;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        string script = "RadioChecked(this)";
        this.RadioButtonCaptchaSelected.Attributes.Add("onclick", script);
    }
 
    public void SetImage(WebPageImage tempWPI)
    {
        wpi = tempWPI;
 
        string imagePath = wpi.ImagePath;
 
        this.ImageCaptcha.ImageUrl = wpi.ImagePath;       
    }
 
    public System.Drawing.Image GetImage()
    {
        return wpi.ActualImage;
    }
 
    public bool CheckBoxState
    {
        get { return this.RadioButtonCaptchaSelected.Checked; }
        set { this.RadioButtonCaptchaSelected.Checked = value; }        
    }
 
    public string URL
    {
        get { return _url; }
        set { _url = value; }
    }
}
 
 
 
 
 
 
 
//COLLECTION OF RADIO BUTTONS MARKUP (ASCX)
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CaptchaRepeater.ascx.cs" Inherits="UserControls_SEO_CaptchaRepeater" %>
 
<%@ Reference Control="~/UserControls/SEO/CaptchaImageDisplay.ascx" %>
 
<div style="vertical-align:top; height: 152px; overflow:auto;width:400px;">
    <asp:PlaceHolder ID="ph" runat="server">
    </asp:PlaceHolder>
</div>
 
 
 
 
 
 
//COLLECTION OF RADIO BUTTONS CODE-BEHIND (ASCX)
[Serializable]
public partial class UserControls_SEO_CaptchaRepeater : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    public void AddCaptchaControlCollection(List<Control> cidColl)
    {
        Table table = new Table();
        TableRow row = new TableRow();
        TableCell cell = null;
 
        int counter = 1;
 
        foreach (Control c in cidColl)
        {
            if ((counter % 4) > 0)
            {                
                cell = new TableCell();
                cell.Controls.Add(c);
                row.Cells.Add(cell);
            }
 
            if ((counter % 4) == 0)
            {
                table.Rows.Add(row);
                row = new TableRow();
            }
 
            counter++;            
        }
 
        ph.Controls.Add(table);
    }
}

Open in new window

mut-exc.jpg
ASP.NET

Avatar of undefined
Last Comment
REA_ANDREW

8/22/2022 - Mon