Introduction

Using a Button and a Div to create radio button pair (Yes / No) on button click. I described my comment on each line which helps to understand the view.
Source Code:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RadioDemo.aspx.cs" Inherits="JavascriptTutorial.RadioDemo"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <script type="text/javascript">
  7. /* Getting Id of Div in which radio button will be add*/
  8. var containerDivClientId = "<%= containerDiv.ClientID %>";
  9. /*variable count uses for define unique Ids of radio buttons and group name*/
  10. var count = 100;
  11. /*This function call by button OnClientClick event and uses for create radio buttons*/
  12. function dynamicRadioButton()
  13. {
  14. /* create a radio button */
  15. var radioYes = document.createElement("input");
  16. radioYes.setAttribute("type", "radio");
  17. /*Set id of new created radio button*/
  18. radioYes.setAttribute("id", "radioYes" + count);
  19. /*set unique group name for pair of Yes / No */
  20. radioYes.setAttribute("name", "Boolean" + count);
  21. /*creating label for Text to Radio button*/
  22. var lblYes = document.createElement("lable");
  23. /*create text node for label Text which display for Radio button*/
  24. var textYes = document.createTextNode("Yes");
  25. /*add text to new create lable*/
  26. lblYes.appendChild(textYes);
  27. /*add radio button to Div*/
  28. containerDiv.appendChild(radioYes);
  29. /*add label text for radio button to Div*/
  30. containerDiv.appendChild(lblYes);
  31. /*add space between two radio buttons*/
  32. var space = document.createElement("span");
  33. space.setAttribute("innerHTML", " ");
  34. containerDiv.appendChild(space);
  35. var radioNo = document.createElement("input");
  36. radioNo.setAttribute("type", "radio");
  37. radioNo.setAttribute("id", "radioNo" + count);
  38. radioNo.setAttribute("name", "Boolean" + count);
  39. var lblNo = document.createElement("label");
  40. lblNo.innerHTML = "No";
  41. containerDiv.appendChild(radioNo);
  42. containerDiv.appendChild(lblNo);
  43. /*add new line for new pair of radio buttons*/
  44. var spaceBr= document.createElement("br");
  45. containerDiv.appendChild(spaceBr);
  46. count++;
  47. return false;
  48. }
  49. </script>
  50. </head>
  51. <body>
  52. <form id="form1" runat="server">
  53. <div>
  54. <asp:Button ID="btnCreate" runat="server" Text="Click Me" OnClientClick="return dynamicRadioButton();" />
  55. <div id="containerDiv" runat="server"></div>
  56. </div>
  57. </form>
  58. </body>
  59. </html>
OUTPUT:
Radio Button.PNG