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);
}
}