2012-03-02 11 views
0

コードビハインドからjqueryダイアログを動的に表示する方法を試してみましたが、これまでのところ幸運はありませんでした。コードビハインドからjqueryダイアログを表示する方法

ユーザーが自分のWebサイトの登録を完了して[ユーザーの作成]ボタンをクリックするとjqueryダイアログボックスが表示されるようにしようとしています。ダイアログに「登録ありがとうございました!」と表示されます。ユーザーが入力したユーザー名がデータベースに存在しない場合

ここに私が持っているものがあり、動作していません。この1つのタスク以外は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); 
     } 

    } 
+0

エラーが発生している場合は、それは何と言いますか?また、$( "#dialog")は複数回使用されていますか? – shanabus

+0

私はちょうどIEの開発者ツールをチェックして、$(function(){ initializedialog(); }}で「オブジェクトが期待されています」というエラーが発生しました。 – jre247

+0

私は$( "#dialog")を複数回使用していません – jre247

答えて

1

また、Response.Redirectも実行しています。別のページに直接行っているため、実際に登録されたスクリプトを表示する時間があるかどうかはわかりません。

どのページにポップアップを表示しますか?

+0

私はresponse.redirectをコメントアウトしましたが、 。私はこのページのコードが配置されているregister.aspxにポップアップを表示します。ありがとう! – jre247

+1

(UpdatePanelを使用して)部分的なポストバックを実行するとdocuent.readyが実行されません。代わりに$(window).load(function(){initializedialog(); showDialog(\ "" + "sb.ToString()+" \ ");})を使用してみてください。 – Ronald

+0

あなたの提案を追加しようとしました。しかし、$(function(){initializedialog();}で "object expected"と言っているエラーが出ます。これを引き起こす原因は何ですか? – jre247

0

ない.remove()ダイアログを実行してください!あなたは代わりに.empty()を探していたでしょうか?

+0

.empty()を実行しようとしましたが、まだダイアログが表示されませんでした:/ – jre247

関連する問題