Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason your radio buttons are not acting as a group is because you have runat="server" which will cause the name and id attributes to be prepended with parents' ids. The following will cause the radio buttons to behave as a cohesive group (note the lack of runat="server"):</p> <p>ascx code:</p> <pre><code>&lt;asp:Repeater ID="uxRadioSelections" runat="server"&gt; &lt;HeaderTemplate&gt; &lt;table border="0" width="300px"&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;tr&gt; &lt;td&gt; &lt;input type="radio" value='&lt;%# Eval("Name") %&gt;' name="HeaderFooter" onclick="alert(this.value);"&gt;&lt;%# Eval("Name") %&gt;&lt;/input&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;/table&gt; &lt;/FooterTemplate&gt; &lt;/asp:Repeater&gt; </code></pre> <p>code behind:</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { string[] names = new string[] {"Alvin", "Simon", "Theodore"}; uxRadioSelections.DataSource = names.Select(x =&gt; new { Name = x }); uxRadioSelections.DataBind(); } </code></pre> <p>However, the following will NOT perform as a cohesive group:</p> <pre><code>&lt;asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"&gt; Test &lt;asp:Repeater ID="uxRadioSelections" runat="server"&gt; &lt;HeaderTemplate&gt; &lt;table border="0" width="300px"&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:RadioButton Text='&lt;%#Eval("Name") %&gt;' runat="server" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;/table&gt; &lt;/FooterTemplate&gt; &lt;/asp:Repeater&gt; &lt;/asp:Content&gt; </code></pre> <p>Due to the naming conventions you alluded to, these individual radio buttons will not find each other. ASP.NET uses RadioButtonLists to overcome this:</p> <p>ascx code:</p> <pre><code>&lt;asp:RadioButtonList ID="uxRadioButtons" runat="server"&gt; &lt;/asp:RadioButtonList&gt; </code></pre> <p>code behind:</p> <pre><code> protected void Page_Load(object sender, EventArgs e) { string[] names = new string[] {"Alvin", "Simon", "Theodore"}; uxRadioButtons.DataTextField = "Name"; uxRadioButtons.DataValueField = "Name"; uxRadioButtons.DataSource = names.Select(x =&gt; new { Name = x }); uxRadioButtons.DataBind(); } </code></pre> <p>This does not give you as granular control, but will provide easy server-side access to the selections. If all you need is javascript-related functionality and increased formatting flexibility, the first format may (which is nearly identical to what you provided) is your best bet. Good luck!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload