52020

How to call a javascript function from a control within a masterpage?

Question:

I have a masterpage with a Login Control in it. When the Login button is clicked, I would like for a JQuery Dialog to popup if the user's membership is about to expire within 30 days, else it will just log them in as normal. I can't figure out how to do it. I wll post parts of code:

Here is the javascript:

<script type="text/javascript"> function showjQueryDialog() { $("#dialog").dialog("open"); } $(document).ready(function() { $("#dialog").dialog({ autoOpen: false, modal: true, buttons: { "Renew Membership": function() { $(this).dialog("close"); } } }); }); </script>

The login button is called ibtnLoginButton and here is part of the code:

//Grab the user profile. UserProfiles userProfile = UserProfiles.GetUserProfiles(txtUserName1.Text); //Calculate the Time Span TimeSpan timeSpan = userProfile.Expiration.Subtract(DateTime.Now); if (timeSpan.Days < 30) { //Show JQuery Dialog Here } else { //Continue with Login Process. }

Answer1:

how about this?

if (timeSpan.Days < 30) { //Show JQuery Dialog Here ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "showExpiration", "showjQueryDialog()", true); }

Answer2:

If, as you've said you've got this jQuery dialog to appear when clicking an asp:Button, why not just hide the button and change your javascript to just press the button once the page has loaded?

Answer3:

Why not always call the jquery method when the button is clicked, and then determine within the javascript method whether or not you want to show the dialogue? If not, just don't do anything. Since you're just checking whether ExpirationDate is smaller than now + 30 days, you can do that calculation just fine in javascript.

Edit:

I can't provide you with the exact solution, but here is some pseudocode to get you on your way.

First make the user profile's expiration date need available in javascript:

<script> var userProfileExpiresOn = "<asp:Literal ID="userProfileExpiresOn" />"; </script>

Then edit your method so that it does the logic you're currently doing server-side for you:

<script> function showjQueryDialog() { if (userProfileExpiresOn < (now + 30 days)) $("#dialog").dialog("open"); } </script>

You can find some documentation on <a href="http://www.w3schools.com/jsref/jsref_obj_date.asp" rel="nofollow">how to work with dates in Javascript at W3schools.</a>

Recommend

  • You've already purchase this subscription. Tap Buy to renew or extend it
  • When is appropriate to use Auto-Renewable Subscriptions - iOS
  • NSRunLoop is receiving a strange selector; possible race condition tomfoolery?
  • Python. How to optimize search functions
  • simplemembership MVC4 get username by userId
  • OAuth 2.0 - When should an access token be renewed with refresh token?
  • How would you audit ASP.NET Membership tables, while recording what user made the changes?
  • jQueryUI dialog replacement for confirm?
  • CSS: How to fix overlapping divs
  • Returning this from a constructor function in JS
  • dismiss Progress Dialog in another Activity … Android
  • How to remove all of a jQuery UI dialog when it is closed
  • Creating a Multi-Step Modal Using Jquery
  • How can I make this modal persistent?
  • Debug.DrawLine not showing in the GameView
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • How do I get HTML corresponding to current DOM tree?
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • Android full screen on only one activity?
  • Django rest serializer Breaks when data exists
  • Alert pop up with LWUIT
  • Recording logins for password protected directories
  • Is there any way to access browser form field suggestions from JavaScript?
  • Optimizing database types to compact database (SQLite)
  • TFS: Get latest causes slow project reloading
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Running a C# exe file
  • Can I make an Android app that runs a web view in Chrome 39?
  • Change an a tag attribute in JavaScript based on screen width
  • Adding custom controls to a full screen movie
  • Getting error when using KSoap library to consume .NET web services
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • How can I use `wmic` in a Windows PE script?