views:

477

answers:

2

This is driving me crazy! I have read at least 5 questions on here closely related to my problem, and probably 5 or so more pages just from googling. I just don't get it.

I am trying to have a jqueryui dialog come up after a user fills out a form saying 'registration submitted' and then redirecting to another page, but I cannot for the life of me get any javascript to work, not even a single alert.

Here is my update panel:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="upForm" runat="server" 
    UpdateMode="Conditional" ChildrenAsTriggers="False">
    <ContentTemplate>
               'Rest of form'
         <asp:Button ID="btnSubmit" runat="server" Text="Submit" /> 
              <p>Did register Pass? <%= registrationComplete %></p>
    </ContentTemplate>
</asp:UpdatePanel>

The Jquery I want to execute: (Right now this is sitting in the head of the markup, with autoOpen set to false)

<script type="text/javascript"> 
function pageLoad() {

    $('#registerComplete').dialog({
        autoOpen: true,
        width: 270,
        resizable: false,
        modal: true,
        draggable: false,
        buttons: {
          "Ok": function() {
          window.location.href = "someUrl";
        }
        }
    });

}
     </script>

Finally my code behind: ( Commented out all the things I've tried)

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click     
      'Dim sbScript As New StringBuilder()'
           registrationComplete = True
            registrationUpdatePanel.Update()

            'sbScript.Append("<script language='JavaScript' type='text/javascript'>" + ControlChars.Lf)'
            'sbScript.Append("<!--" + ControlChars.Lf)'
            'sbScript.Append("window.location.reload()" + ControlChars.Lf)'
            'sbScript.Append("// -->" + ControlChars.Lf)'
            'sbScript.Append("</")'
            'sbScript.Append("script>" + ControlChars.Lf)'

            'ScriptManager.RegisterClientScriptBlock(Me.Page, Me.GetType(), "AutoPostBack", sbScript.ToString(), False)'
            'ClientScript.RegisterStartupScript("AutoPostBackScript", sbScript.ToString())'
                            'Response.Write("<script type='text/javascript'>alert('Test')</script>")'
            'Response.Write("<script>windows.location.reload()</script>")'
End Sub

I've tried:

  • Passing variables from server to client via inline <%= %> in the javascript block of the head tag.

  • Putting that same code in a script
    tag inside the updatePanel.

  • Tried to use
    RegisterClientScriptBlock and
    RegisterStartUpScript

  • Just doing a Response.Write with the script tag written in it.

  • Tried various combinations of putting the entire jquery.dialog code in the registerstartup script, or just
    trying to change the autoOpen
    property, or just calling "open" on
    it.

I can't even get a simple alert to work with any of these, so I am doing something wrong but I just don't know what it is.

Here is what I know:

  • The Jquery is binding properly even on async postbacks, because the div container that is the dialog box is always invisible, I saw a similiar post on here stating that was causing an issue, this isn't the case here.

  • Using page_load instead of
    document.ready since that is supposed to run on both async and normal
    postbacks, so that isn't the issue.

  • The update panel is updating correctly because <p>Did register Pass? <%= registrationComplete %></p> updates to true after I submit the form.

So how can I make this work? All I want is -> click submit button inside an update panel -> run server side code to validate form and insert into db -> if everything succeeded, have that jquery (modal) dialog pop up saying hey it worked.

+1  A: 

You may checkout the endRequest event:

function pageLoad() {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function(sender, e) {
        // this function will be executed after the update panel finished 
        // the AJAX request, so here you may open your jQuery dialog

        if (e.get_error()) {

        } else {

        }
    });
}
Darin Dimitrov
So how would I pass it a variable like I did above? The problem I ran into is that the variable I pass in <%= %> only runs once when the page is loaded. Any subsequent async postbacks might run that code, but it wouldn't update to the newest value of registrationComplete. What is E in your example? The page? So in your example I would have a get_error() function in the code behind and it would work? Or would that be in the Javascript? If in the Javascript, I still run into the issue of giving it the updated value.
AverageJoe719
+1  A: 

To register from the Codebehind, here's the format:

ClientScript.RegisterStartupScript(GetType(), "AutoPostBackScript", 
                                   "alert('hi');", True);

The true on the end tells it to wrap your code in <script> tags, leave it off if you want to do the tags yourself.

Nick Craver
@Nick: Not sure why but registering javascript on async postbacks never worked to me unless I use as parameter the type of the update panel (UpdatePanel1.GetType())
Claudio Redi
@Claudio - Depends where it's being called from, should be called from a control that's participating in the update, the update panel itself isn't a bad type to stick it on, so yes that works :)
Nick Craver
you should be using `ScriptManager` instead of `ClientScript` for pages with `UpdatePanels`.
lincolnk
@Nick Craver, you can't just call GetType()
AverageJoe719
@AverageJoe719 - Sure you can - It's `Page.GetType()` that you're calling, just like you don't need to call `Page.Response.End()`, it's just `Response.End()`.
Nick Craver
@Nick Craver, that's odd, When I tried doing that, it gave me an error, so I just typed Page.GetType() as you said and that worked. Using VB not sure if that mattered. But I figured it out and it works with `ScriptManager.RegisterStartupScript(Me, myUpdatePanel.GetType(), "AutoPostBackScript", "alert('Hi');", True)` Thanks.
AverageJoe719
I had the same problem and I had to use `ScriptManager` instead of `ClientScript`, and I had to pass the `UpdatePanel` and its type. `ScriptManager.RegisterClientScriptBlock(mycontrol, mycontrol.GetType(), "callback", "alert('ho')", true);`
BrunoLM