LoginProfileControl.ascx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <%@ Control Language="C#" ClassName="LoginProfileControl" %>
  2. <%@Import Namespace="System.Web.UI" %>
  3. <script language="C#" runat="server">
  4. protected void Page_(object sender, EventArgs e)
  5. {
  6. ScriptManager sm = ScriptManager.GetCurrent(this.Page);
  7. if (sm == null)
  8. {
  9. throw new InvalidOperationException(
  10. "Script Manager not defined. Create one in the page where you use this control.");
  11. }
  12. }
  13. </script>
  14. <script type="text/javascript">
  15. // Profile.js
  16. // The OnClickLogin function is called when
  17. // the user clicks the Login button.
  18. // It calls the AuthenticationService.login to
  19. // authenticates the user.
  20. function OnClickLogin()
  21. {
  22. Sys.Services.AuthenticationService.login(
  23. document.form1.userId.value,
  24. document.form1.userPwd.value,false,null,null,
  25. OnLoginComplete, OnAuthenticationFailed,
  26. "User context information.");
  27. }
  28. // The OnClickLogout function is called when
  29. // the user clicks the Logout button.
  30. // It logs out the current authenticated user.
  31. function OnClickLogout()
  32. {
  33. Sys.Services.AuthenticationService.logout(
  34. null, OnLogoutComplete, OnAuthenticationFailed,null);
  35. }
  36. function OnLogoutComplete(result,
  37. userContext, methodName)
  38. {
  39. // Code that performs logout
  40. // housekeeping goes here.
  41. }
  42. // This function is called after the user is
  43. // authenticated. It loads the user's profile.
  44. // This is the callback function called
  45. // if the authentication completed successfully.
  46. function OnLoginComplete(validCredentials,
  47. userContext, methodName)
  48. {
  49. if(validCredentials == true)
  50. {
  51. DisplayInformation("Welcome " + document.form1.userId.value);
  52. LoadProfile();
  53. // Hide or make visible page display elements.
  54. GetElementById("loginId").style.display = "none";
  55. GetElementById("setProfileButton").style.display ="block";
  56. GetElementById("logoutId").style.display = "block";
  57. }
  58. else
  59. {
  60. DisplayInformation("Could not login");
  61. }
  62. }
  63. // This is the callback function called
  64. // if the authentication failed.
  65. function OnAuthenticationFailed(error_object,
  66. userContext, methodName)
  67. {
  68. DisplayInformation("Authentication failed with this error: " +
  69. error_object.get_message());
  70. }
  71. // Loads the profile of the current
  72. // authenticated user.
  73. function LoadProfile()
  74. {
  75. Sys.Services.ProfileService.load(null,
  76. OnLoadCompleted, OnProfileFailed, null);
  77. }
  78. // Saves the new profile
  79. // information entered by the user.
  80. function SaveProfile()
  81. {
  82. Sys.Services.ProfileService.properties.Backgroundcolor =
  83. GetElementById("bgcolor").value;
  84. // document.getElementById('bgcolor').value;
  85. Sys.Services.ProfileService.properties.Foregroundcolor =
  86. GetElementById("fgcolor").value;
  87. // document.getElementById('fgcolor').value;
  88. Sys.Services.ProfileService.save(null,
  89. OnSaveCompleted, OnProfileFailed, null);
  90. }
  91. // Reads the profile information and displays it.
  92. function OnLoadCompleted(numProperties, userContext, methodName)
  93. {
  94. document.bgColor =
  95. Sys.Services.ProfileService.properties.Backgroundcolor;
  96. document.fgColor =
  97. Sys.Services.ProfileService.properties.Foregroundcolor;
  98. }
  99. // This is the callback function called
  100. // if the profile was saved successfully.
  101. function OnSaveCompleted(numProperties, userContext, methodName)
  102. {
  103. LoadProfile();
  104. // Hide the area that contains
  105. // the controls to set the profile properties.
  106. SetProfileControlsVisibility("none");
  107. }
  108. // This is the callback function called
  109. // if the profile load or save operations failed.
  110. function OnProfileFailed(error_object, userContext, methodName)
  111. {
  112. alert("Profile service failed with message: " +
  113. error_object.get_message());
  114. }
  115. // Utility functions.
  116. // This function sets the visibilty for the
  117. // area containing the page elements for settings
  118. // profiles.
  119. function SetProfileControlsVisibility(currentVisibility)
  120. {
  121. GetElementById("setProfileProps").style.display =
  122. currentVisibility;
  123. }
  124. // Utility function to display user's information.
  125. function DisplayInformation(text)
  126. {
  127. document.getElementById('feedBackId').innerHTML =
  128. "<br/>"+ text;
  129. }
  130. function GetElementById(elementId)
  131. {
  132. var element = document.getElementById(elementId);
  133. return element;
  134. }
  135. if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
  136. </script>
  137. <script language="C#" runat="server">
  138. protected void Page_PreLoad(object sender, EventArgs e)
  139. {
  140. ScriptManager sm = ScriptManager.GetCurrent(this.Page);
  141. if (sm == null)
  142. {
  143. // throw new InvalidOperationException("sm is null");
  144. sm = new ScriptManager();
  145. Page.Controls.Add(sm);
  146. }
  147. }
  148. </script>
  149. <center>
  150. <!-- Login form -->
  151. <div id="loginId" style="display:block;">
  152. <table id="loginForm">
  153. <tr>
  154. <td style="background-color:Yellow; font-weight:bold; color:Red">User Name:</td>
  155. <td><input type="text"
  156. id="userId" name="userId" value=""/></td>
  157. </tr>
  158. <tr>
  159. <td style="background-color:Yellow; font-weight:bold; color:Red">Password:</td>
  160. <td><input type="password"
  161. id="userPwd" name="userPwd" value="" /></td>
  162. </tr>
  163. <tr>
  164. <td align="center" colspan="2">
  165. <input type="button"
  166. id="login" name="login" value="Login"
  167. onclick="OnClickLogin()" /></td>
  168. </tr>
  169. </table>
  170. </div>
  171. <!-- Set profile button -->
  172. <div id="setProfileButton" style="display:none">
  173. <input type="button"
  174. value="Set Profile Properties"
  175. onclick="SetProfileControlsVisibility('block')"/>
  176. </div>
  177. <!-- User feedback -->
  178. <div id="feedBackId" style="display:block"></div>
  179. <br />
  180. <!-- Logout button -->
  181. <input id="logoutId" type="button"
  182. value="Logout" style="display:none"
  183. onclick="OnClickLogout()" />
  184. <!-- Set profile properties form -->
  185. <div id="setProfileProps" style="display:none" >
  186. <table>
  187. <tr>
  188. <td align="left">Foreground Color</td>
  189. <td align="left"><input type="text" id="fgcolor"
  190. name="fgcolor" value=""/></td>
  191. </tr>
  192. <tr>
  193. <td align="left">Background Color</td>
  194. <td align="left"><input type="text" id="bgcolor"
  195. name="bgcolor" value="" /></td>
  196. </tr>
  197. <tr>
  198. <td align="center" colspan="2"><input type="button"
  199. id="saveProf" name="saveProf"
  200. value="Save Profile Properties"
  201. onclick="SaveProfile();" /></td>
  202. </tr>
  203. </table>
  204. </div>
  205. </center>
  206. <hr />
  207. <div id="FeedBackID" style="display:none" />