Introduction

Here we create a simple example of DynamicPopulate. In this example when we select the particular date format, the TextBox will show the result on the basis of the current Date.
DynaJava.gif
Step 1: First we take four radio buttons and a TextBox in our Form.
  1. <input id="rdonormal" type="radio" name="group1" value="Normal" />Normal
  2. <br />
  3. <input id="rdoshortdate" type="radio" name="group1" value="Short Date" />Short Date
  4. <br />
  5. <input id="rdolongdate" type="radio" name="group1" value="Long Date" /> Long date
  6. <br />
  7. <input id="rdoutcdatetime" type="radio" name="group1" value="UTCDateTime" />UTC Date/Time
  8. <br />
  9. <br />
  10. <input type="text" id="txtdate" style=" text-align:center; width: 278px;" />
Note: Here name is used to tell the group name. When we select one button, the other button of the other groups are unselected.
Step 2: Now we write JavaScript Functions:
(a): Here we write the code for the first radio button (rdonormal); when we click on it, it shows the date according to the js function(Show()):
  1. <input id="rdonormal" type="radio" name="group1" value="Normal" onclick="Show()" />
JavaScript Function
  1. function Show() {
  2. var a = new Date();
  3. document.getElementById('txtdate').value = a.toLocaleString();
  4. }
Here the toLocaleString() method is used to convert the date to a string according to the Locale.
(b) ): Here we write the code for the first radio button (rdoshortdate); when we click on it, it shows the date according to the js function(ShowShortDate()):
  1. <input id="rdoshortdate" type="radio" name="group1" value="Short Date" onclick="ShowShortDate()" />Short Date
JavaScript Function
  1. function ShowShortDate() {
  2. var a = new Date();
  3. document.getElementById('txtdate').value = a.getMonth() + "/" + a.getDate() + "/" + a.getYear();
  4. }
Here we use the getDate(), getMonth() and getYear() functions to get the current Date, Month and Year.
(c) ): Here we write the code for the first radio button (rdolongdate); when we click on it, it shows the date according to the js function(ShowLongDate()):
  1. <input id="rdolongdate" type="radio" name="group1" value="Long Date" onclick="ShowLongDate()"/> Long date
JavaScript Function
  1. function ShowLongDate() {
  2. var a = new Date();
  3. document.getElementById('txtdate').value = a.toLocaleDateString();
  4. }
Here the tolocaleDateString() method returns the date portion as a string as per the Locale.
(d) ): Here we write the code for the first radio button (rdoutcdatetime); when we click on it, it shows the Date according to the js function(ShowUTCDateTime()):
  1. <input id="rdoutcdatetime" type="radio" name="group1" value="UTCDateTime" onclick="ShowUTCDateTime()"/>UTC Date/Time
JavaScript Function
  1. function ShowUTCDateTime() {
  2. var a = new Date();s
  3. document.getElementById('txtdate').value = a.toUTCString();
  4. }
Here ToUTCString() converts the date to a string, according to the Coordinated Universal Time (UTC).
Complete Program
  1. <head runat="server">
  2. <script language="javascript" type="text/javascript">
  3. function Show() {
  4. var a = new Date();
  5. document.getElementById('txtdate').value = a.toLocaleString();
  6. }
  7. function ShowShortDate() {
  8. var a = new Date();
  9. document.getElementById('txtdate').value = a.getMonth() + "/" + a.getDate() + "/" + a.getYear();
  10. }
  11. function ShowLongDate() {
  12. var a = new Date();
  13. document.getElementById('txtdate').value = a.toLocaleDateString();
  14. }
  15. function ShowUTCDateTime() {
  16. var a = new Date();
  17. document.getElementById('txtdate').value = a.toUTCString();
  18. }
  19. </script>
  20. <title></title>
  21. <style type="text/css">
  22. #rdonormal
  23. {
  24. width: 24px;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <form id="form1" runat="server">
  30. <div>
  31. <input id="rdonormal" type="radio" name="group1" value="Normal" onclick="Show()" />Normal
  32. <br />
  33. <input id="rdoshortdate" type="radio" name="group1" value="Short Date" onclick="ShowShortDate()" />Short Date
  34. <br />
  35. <input id="rdolongdate" type="radio" name="group1" value="Long Date" onclick="ShowLongDate()"/> Long date
  36. <br />
  37. <input id="rdoutcdatetime" type="radio" name="group1" value="UTCDateTime" onclick="ShowUTCDateTime()"/>UTC Date/Time
  38. <br />
  39. <br />
  40. <input type="text" id="txtdate" style=" text-align:center; width: 278px;" />
  41. </div>
  42. </form>
  43. </body>