views:

391

answers:

5

I have the following 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>

I have an asp:Button on the page which logs the user it. This is the sample of what I want to occur when the button is clicked on the server-side:

protected void LoginButton_OnClick(object sender, EventArgs e)
{
    UserProfile profile = UserProfile.GetUserProfile(txtUserName.Text);

    TimeSpan ts = profile.Expiration.Subtract(DateTime.Now);

    if(ts.Days <= 30)
      //call showJQueryDialog() to open the dialog box
      Page.ClientScript.RegisterStartupScript(typeof(Login2), "showjquery",   
      "showJQueryDialog();", true);
    else
     //log the user in as normal.

}

Also is how would I attach a method such as the following to the Renew Button on the Dialog

public void Renew()
{
    Response.Redirect("Renew.aspx");
}
+2  A: 
if(ts.Days <= 30)
    ScriptManager.RegisterStartupScript(
        typeof(MyPage), "showjquery", 
        "$(document).ready(function() { showJQueryDialog(); };", 
        true
    )
else
     //log the user in as normal.

Put that right where you have: //call showJQueryDialog() to open the dialog box

Update 1: You seem to be using an update panel, in that case you need to use ScriptManager.RegisterStartupScript

Update 2: You also want to wrap the js call in a jquery .ready call, so it isn't triggered before the dialog has been configured. This is better than hooking up the body onload because onload waits for images to be loaded so .ready will show sooner (depending on the images and other bits of info loaded).

eglasius
Where would I put this code?
Xaisoft
in LoginButton_OnClick()
tsilb
How would I handle my if statement, what would I put in there?
Xaisoft
@Xaisoft right where you have the call showjquery comment
eglasius
updated the answer, with the surrounding if/else
eglasius
ok, i will try it. Do yo know what namespace ClientScript is in and what is the MyPage and "showjquery" parameters.
Xaisoft
its a property of the page: http://msdn.microsoft.com/en-us/library/system.web.ui.page.clientscript(VS.80).aspx - for info on the params: http://msdn.microsoft.com/en-us/library/z9h4dk8y(VS.80).aspx
eglasius
I put this in the if statement, but the dialog does not popup. Login2 is the name of my Page. Page.ClientScript.RegisterStartupScript(typeof(Login2), "showjquery", "showJQueryDialog();", true);
Xaisoft
first check your showjQueryDialog javascript is working well - view the html source after the login click in the browser and you should see the showJQueryDialog is in there.
eglasius
I actually don't see it, does it matter if I am adding the script to a control on the page.
Xaisoft
Default.aspx has a control called Login2.ascx and I am adding it to Login2.ascx.cs.
Xaisoft
I don't see the showJQueryDialog in the bottom of the page when I view source.
Xaisoft
You are surely using ajax then, use scriptmanager instead, check http://stackoverflow.com/questions/646140/jqmodal-isnt-working-in-the-updatepanel
eglasius
+1  A: 

I would keep a hidden LinkButton and then call the __doPostBack method in javascript.

<asp:LinkButton runat="server" ID="Renew" OnClick="Renew_Click" style="display:none" />

jQuery

$(document).ready(function() {
    $("#dialog").dialog({
       autoOpen: false,
       modal: true,
       buttons: { "Renew Membership": function() { 
          $(this).dialog("close"); 
          __doPostBack('Renew', '');
         // or if inside a master page something like this
          __doPostBack('ctl00$ContentPlaceHolder1$Renew', '');
       } }
    });
});
bendewey
+3  A: 

As calling client side function is not possible I would suggest to emit in javascript the information required for the decision and make everything happen on the client side.

Alternatively you can do need a page reload, as suggested from previous commenter.

devdimi
@devdimi you can send the js to call the showjQueryDialog
eglasius
+1  A: 

I really don't understand Freddy's approach to this at all. I am misunderstanding something maybe. The way I see it, there are only two possibilities here, as devdimi point out. Either:

a) Do all the logic in the client-side onClick javascript. You could call an AJAX method that performs the action in the server-side OnClick, then call your jQuery popup in the AJAX callback.

b) Do a postback, handle the server-side OnClick, then attach javascript for the page that runs in the body onLoad event:

body.Attributes.Add("onLoad", "showJQueryDialog();")

Bryan
@Bryan option b is the equivalent of mine - sending js to execute on the client, see my second update on using .ready instead. Regarding option a, he didn't mentioned he was using ajax initially (kind of / update panel), I am unsure if you need to do anything special though as he is login the user.
eglasius
OK, now that I've read the docs I see it. For some reason I was under the mistaken impression that RegisterStartupScript was badly named and didn't actually execute your script in the onLoad.
Bryan
@Bryan it doesn't technically put it on the onload, it sets it at the end of the page - I updated the rendered script so it attaches to the .ready of the jquery (which also not technically uses the onload, but does waits for the DOM to be ready)
eglasius
A: 

I have a somewhat similar issue with IE8.

We're using ASP.NET and anytime we do a Response.Redirect within the PageLoad/Control-Events IE8 sets all the base DOM objects to undefined (Image, window, document) But if we do the redirect during the PreInit event then IE8 is fine.. Lovely

Justin