如何从代码后面显示jquery对话框

时间:2012-03-02 18:37:18

标签: jquery

我在网上搜索了一段时间试图弄清楚如何从代码隐藏中动态显示jquery对话框,到目前为止我没有太多运气。

我正在尝试在用户注册完网站并点击“创建用户”按钮后弹出一个jquery对话框。我只希望对话框弹出“感谢您注册!”如果用户输入的用户名尚未存在于数据库中。

这是我拥有的,不起作用的。我可以将jquery与其他东西一起使用,除了这一项任务。有帮助吗?非常感谢!

<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>

代码背后:

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

    }

2 个答案:

答案 0 :(得分:1)

你也在做一个Response.Redirect。不确定这是否真的有时间显示任何已注册的脚本,因为您将直接转到另一个页面。

您想在哪个页面上显示弹出窗口?

答案 1 :(得分:0)

不要.remove()对话框!也许您正在寻找.empty()