0

I've searched online for awhile trying to figure out how to dynamically display a jquery dialog from codebehind, and I haven't had much luck so far.

I'm trying to have a jquery dialog box pop up after a user is finished registering for my website and clicks the "create user" button. I only want the dialog to pop up with "Thank you for registering!" if the username the user entered doesn't already exist in the database.

Here's what I have and is not working. I'm able to use jquery with other things, except for this one task. Any help? Thanks so much!

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>


<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <asp:Label ID="lblError" ForeColor="Red" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
 <asp:AsyncPostBackTrigger ControlID="CreateUserButton" EventName="Click" />
</Triggers>
</asp:UpdatePanel>

<script type="text/javascript">
    $(function () {
        initializedialog();
    });
    function initializedialog() {
        $("#dialog").dialog({
            autoOpen: false,
            hide: 'blind',
            minHeight: 125,
            maxWidth: 300,
            show: 'blind',
            title: 'Thanks!'
        });
    }

    //This function is called from the script injected from code-behind.
    function showDialog(message) {
        $("#dialog").remove();
        $("#dialog").append(message);
        $("#dialog").dialog('open');
    }
</script>

<div id="pnlpopup">
            <p class="submitButton">
                <asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="Create User" 
                 OnClick="CreateUserButton_Click" 
                    ValidationGroup="RegisterUserValidationGroup" SkinID="btnLoginRegister" 
                    Height="29px" Width="107px" />
            </p>
</div>

<div id="dialog" style="display: none">

<asp:Label ID="lblMessage" runat="server" Text="Thank you registering!">
</asp:Label>

</div>

Code behind:

protected void CreateUserButton_Click(object sender, EventArgs e)
    {
        bool bStatus = false;
        DataTable dt = new DataTable();
        string strRedirect = "";
        DataRow dr = null;

        //retrieve userInput fields
        string stringUserName = UserName.Text;
        string stringPassword = Password.Text;
        string stringConfirmPassword = ConfirmPassword.Text;

        //set database user role to default
        string userRole = "db_datawriter";

        //check if username already exists in database
        dr = Data_Access_Management.DataAccess.GetUser(stringUserName);

        if (dr != null) // Check if the DataRow returns any data from database
        {
            lblError.Text = "That Username already exists.";

            bStatus = true;
        }

        if (!bStatus)
        {
            //insert user into user table in database
            Data_Access_Management.DataAccess.InsertUser(stringUserName, stringPassword, userRole);

            strRedirect = CommonStrings.SessionLoginPage;

            //string script = "$('#dialog').dialog('open');";
            //ClientScript.RegisterStartupScript(GetType(), "alert('foo');", script, true);

            StringBuilder sb = new StringBuilder();
            string script = "$(function(){initializedialog();showDialog(\"" + sb.ToString() + "\");});";
            ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "dialog", script, true);

            //redirect to contact.aspx
            Response.Redirect(strRedirect);
        }

    }
3
  • Do you get an error and if so what does it say? Also, is $("#dialog") used more than once? Commented Mar 2, 2012 at 18:52
  • I just checked IE developer tools and I have an "object expected" error at $(function () { initializedialog(); }); Commented Mar 2, 2012 at 19:25
  • I'm also not using $("#dialog") more than once Commented Mar 2, 2012 at 19:26

2 Answers 2

1

You're also doing a Response.Redirect. Not sure if this will actually have time to display any registered scripts because you're going directly to another page.

On which page do you want to show the popup?

Sign up to request clarification or add additional context in comments.

4 Comments

I tried commenting out the response.redirect and still the popup didn't work. I want the popup to display on register.aspx, which is where the code for this page is located. Thanks!
docuent.ready does not get executed when performing a partial postback (through UpdatePanel). Try using $(window).load(function(){initializedialog();showDialog(\"" + sb.ToString() + "\");}) instead.
I tried adding your suggestions. However, I'm getting an error saying "object expected" at $(function () { initializedialog(); }); Any idea what's causing this?
Because you are working with updatepanels, the dialog is already initialized the first time your page is loaded. You don't need to call the initializeDialog again but just the showdialog function.
0

Don't .remove() the dialog! Maybe you were looking for .empty() instead?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.