2012-08-08 35 views
3

正在asp.net web應用程序中使用下面的消息框。現在我想將此消息框轉換爲確認消息框,並在確實消息框中執行某些操作,否則表示拒絕該應用程序。web應用程序中的確認消息框

ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Are you sure, you want to apply?');</script>", false); 
+2

確認你想要做什麼客戶端函數或服務器端調用? – yogi 2012-08-08 09:15:13

+0

服務器端通話 – 2012-08-08 09:19:41

回答

6

我認爲你是這樣做的錯誤方式。您應該在回覆之前顯示此確認,然後僅在他們選擇「應用」時回覆。

使用ASP.NET Web控件,該Button控制具有OnClientClick屬性,該屬性可以用來調用JavaScript HTTP POST之前:

你可以做這樣的事情:

<asp:button id="btn" 
      runat="server" 
      Text="Apply" 
      OnClientClick="return confirm('Are you sure you wish to apply?');" 
      OnClick="btn_Click" /> 
+2

完全同意! – 2012-08-08 09:20:36

+0

這是我的工作..然後我怎麼可以在確認消息中使用這樣的換行符,如「返回確認('你確定
你想申請?');「 – 2012-08-08 09:49:05

+0

我相信你用'\ n'代替'
' – Curt 2012-08-08 10:01:17

2

寄存器腳本波紋管代替警報

<script type="text/javascript"> 
var r=confirm("Are you sure you are sure?") 
if (r==true) 
{ 
    //You pressed OK! 
} 
else 
{ 
    //You pressed Cancel! 
} 
</script> 
0

等效於確認框的JavaScript是confirm()方法。此方法根據用戶的「確定」或「取消」按鈕響應返回true或false值。

用法:

var confirmed = confirm('Are you sure?','Are you sure you want to delete this item?'); 

if(confirmed){ 
    //do something 
} else { 
    //do something else 
} 
0

試試這個

添加這對你的CS文件來顯示,而不是confirmalert

string confirm = 
"if(confirm('Are you surely want to do this ??')) __doPostBack('', 'confirmed');"; 
ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", confirm, true); 

添加此同一頁上,檢查時用戶來自確認框

protected void Page_Load(object sender, EventArgs e) 
{ 
    string parameter = Request["__EVENTARGUMENT"]; 
    if (string.Equals("confirmed", 
         parameter, 
         StringComparison.InvariantCultureIgnoreCase)) 
    { 
     // Call your server side method here 
    } 
} 

爲此,我使用了__doPostBack,您可以從here瞭解更多信息。 希望它會幫助你

0

嘗試OnClientClick事件的代碼檢查時對按鈕用戶點擊

<script type="text/javascript"> 
     function check() 
    {   
     var chk =confirm("Are you sure you are sure?") 
     if (chk==true) 
     { 
      // try you want 
     } 
     else 
     { 
      // try you do not want 
     } 
     return true; 
    } 

</script>   

<asp:button id="Button1" 
     runat="server" 
     Text="Button1" 
     OnClientClick="return check();"/> 
0
private void showMessage(string msg){ 

     ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('"+ msg +"');</script>", false); 

保護無效BtnReg_Click(對象發件人,EventArgs的){

 OracleHelper.OracleDBOpen(); 
     object flag = OracleHelper.OracleExecuteScalar("your select Query "); 
     if (flag == null) 
     { 
         showMessage("Failed !!! "); 

     } 
     else 
     { 
      string reg = String.Format("your Insert Query "); 

      showMessage("successfuly"); 
      OracleHelper.OracleExecuteNonQuery(reg); 

     }     
     OracleHelper.OracleDBClose(); 
    } 
} 
0

要在C#中完成此操作,您可以試試這個:

protected override void OnInit(EventArgs e) 
    { 
     AddConfirmationButton(); 
     base.OnInit(e); 
    } 

    private void AddConfirmationButton() 
    { 
     Button confirmButton = new Button(); 
     confirmButton.Text = "Action Foo"; 
     string confirmationMessage = "Are you sure you wish to do action Foo?"; 
     confirmButton.OnClientClick = "return confirm('" + confirmationMessage + "');"; 
     confirmButton.Command += confirmButton_Command; 

     Controls.Add(confirmButton); 

    } 

    void confirmationMessage_Command(object sender, CommandEventArgs e) 
    { 
     DoActionFoo(); //work your magic here. 
    } 

這會從網頁向用戶呈現和「確定/取消」對話框。如果用戶點擊「確定」,則命令事件中的功能將觸發。如果用戶點擊「取消」,則不會發生任何事情。

0
ScriptManager.RegisterStartupScript(page,this.GetType(), "temp","javascript:calopen(); 
",true); 

function calopen()  
{  
    if (confirm("Are you sure?"+'\n'+"Are you want to delete"))  
    { 
     enter code here  
    } 
    else 
    { 
     return false; 
    }  
}