Search code examples
asp.netradiobuttonlist

Automatically select item in RadioButtonList after clicking TextBox


So I currently have a page with two radio buttons from a RadioButtonList followed by two TextBoxes:

<asp:RadioButtonList ID="radioButtonList1" runat="server">
    <asp:ListItem Value="Option1">Option1</asp:ListItem>
    <asp:ListItem Value="Option2">Option2</asp:ListItem>
</asp:RadioButtonList>

<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>

Is there an easy way how I can make the first list item of the RadioButtonList selected after clicking on the first TextBox? This would be more user friendly since it is common to forget to check a value before continuing.

Thanks in advance.


Solution

  • After a day of research I found the answer myself. In the code behind inside the Page_Load function I put this line of code:

    TextBox1.Attributes.Add("OnClick", "document.getElementById('" + radioButtonList1.ClientID + "_0').checked=true;");
    

    I am pretty sure there is a better way but for now this seems to work just fine.