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:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RadioDemo.aspx.cs" Inherits="JavascriptTutorial.RadioDemo"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script type="text/javascript">
-
-
- var containerDivClientId = "<%= containerDiv.ClientID %>";
-
-
- var count = 100;
-
-
- function dynamicRadioButton()
- {
-
- var radioYes = document.createElement("input");
- radioYes.setAttribute("type", "radio");
-
-
- radioYes.setAttribute("id", "radioYes" + count);
-
-
- radioYes.setAttribute("name", "Boolean" + count);
-
-
- var lblYes = document.createElement("lable");
-
-
- var textYes = document.createTextNode("Yes");
-
-
- lblYes.appendChild(textYes);
-
-
- containerDiv.appendChild(radioYes);
-
-
- containerDiv.appendChild(lblYes);
-
-
- var space = document.createElement("span");
- space.setAttribute("innerHTML", " ");
- containerDiv.appendChild(space);
-
- var radioNo = document.createElement("input");
- radioNo.setAttribute("type", "radio");
- radioNo.setAttribute("id", "radioNo" + count);
- radioNo.setAttribute("name", "Boolean" + count);
-
- var lblNo = document.createElement("label");
- lblNo.innerHTML = "No";
- containerDiv.appendChild(radioNo);
- containerDiv.appendChild(lblNo);
-
- var spaceBr= document.createElement("br");
- containerDiv.appendChild(spaceBr);
-
- count++;
- return false;
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="btnCreate" runat="server" Text="Click Me" OnClientClick="return dynamicRadioButton();" />
- <div id="containerDiv" runat="server"></div>
- </div>
- </form>
- </body>
- </html>
OUTPUT: