Default.aspx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <%@ Page Language="C#" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  3. "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head id="Head1" runat="server">
  6. <title>Custom Events Example</title>
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <asp:ScriptManager ID="ScriptManager1" runat="server" >
  11. <Scripts>
  12. <asp:ScriptReference Path="question.js" />
  13. <asp:ScriptReference Path="section.js" />
  14. </Scripts>
  15. </asp:ScriptManager>
  16. <script type="text/javascript">
  17. // Add handler to init event
  18. Sys.Application.add_init(appInitHandler);
  19. function appInitHandler() {
  20. // create components
  21. $create(Demo.Question, {correct: '3'},
  22. {select: onAnswer},null, $get('Question1'));
  23. $create(Demo.Question, {correct: '3'},
  24. {select: onAnswer},null, $get('Question2'));
  25. $create(Demo.Question, {correct: '3'},
  26. {select: onAnswer},null, $get('Question3'));
  27. $create(Demo.Question, {correct: '3'},
  28. {select: onAnswer},null, $get('Question4'));
  29. $create(Demo.Section, null,
  30. {complete: onSectionComplete},null, $get('group1'));
  31. $create(Demo.Section, null,
  32. {complete: onSectionComplete},null, $get('group2'));
  33. }
  34. function onAnswer(question) {
  35. // If all questions in this section answered,
  36. // raise complete event
  37. var section = question.get_element().parentElement;
  38. var questions = section.children;
  39. for (var i=0; i<questions.length; i++) {
  40. if (questions[i].selectedIndex === -1) {
  41. return;
  42. }
  43. }
  44. $find(section.id).raiseComplete();
  45. }
  46. function onSectionComplete(section) {
  47. // Change background color of <div>.
  48. section.get_element().style.backgroundColor = 'yellow';
  49. }
  50. function done() {
  51. // Display correct answers where needed.
  52. var c = Sys.Application.getComponents();
  53. var s = "";
  54. for (var i=0; i<c.length; i++) {
  55. var type = Object.getType(c[i]).getName();
  56. if (type !== 'Demo.Question') continue;
  57. var element = c[i].get_element()
  58. var answer = element.selectedIndex
  59. var correct = $find(c[i].get_id()).get_correct();
  60. if (answer !== correct) {
  61. var s = 'The correct answer is ';
  62. s += element.options[correct].innerText;
  63. element.outerHTML += s;
  64. }
  65. }
  66. }
  67. </script>
  68. <h3>Basic Addition</h3><br />
  69. <div id="Group1">
  70. 2 + 2 =
  71. <select id="Question1" >
  72. <option>2</option>
  73. <option>22</option>
  74. <option>4</option>
  75. <option>5</option>
  76. </select><br />
  77. 2 + 3 =
  78. <select id="Question2" >
  79. <option>3</option>
  80. <option>23</option>
  81. <option>5</option>
  82. <option>6</option>
  83. </select><br />
  84. </div><br /> <br />
  85. <h3>Basic Subtraction</h3><br />
  86. <div id="Group2">
  87. 2 - 1 =
  88. <select id="Question3" >
  89. <option>2</option>
  90. <option>0</option>
  91. <option>1</option>
  92. <option>-2</option>
  93. </select><br />
  94. 2 - 2 =
  95. <select id="Question4" >
  96. <option>2</option>
  97. <option>-2</option>
  98. <option>0</option>
  99. <option>-4</option>
  100. </select><br />
  101. </div><br /><br />
  102. <h3>Finished! </h3>
  103. <input id="Submit1" type="button" value="submit" onclick="done();" />
  104. </form>
  105. </body>
  106. </html>