2013-10-23 46 views
0

我想在按鈕單擊時生成一個警告框。我已經這樣寫了如何從alertbox中獲取OK和CANCEL的值

protected void btn_submit_click(object sender, ImageClickEventArgs e) 
{ 
    btn_submit.OnClientClick = @"return confirm('Student has not completed all the steps? Are you sure you want to submit the details?');"; 
        bool type = false; 
    if(type==true) 
{ 
    //If clicks OK button 
    } 
else 
{//If clicks CANCEL button 
} 
} 

警報框正確無誤。但是,我怎麼能從代碼中獲取值?請幫助。

+0

if(type- = true)oops這裏是錯誤的,如果(type == true)更正它 – Shadow

+0

爲什麼不使用'ClientScriptMan憤怒' – Nag

回答

3

當確認返回false時,由於javascript中的click事件被取消,所以沒有回傳。如果你想有點擊後回傳取消你需要改變你的代碼一點點:

服務器端:

protected void Page_Load(object sender, System.EventArgs e) 
{ 
    btn_submit.Click += btn_submit_click; 
    btn_submit.OnClientClick = @"return getConfirmationValue();"; 
} 

protected void btn_submit_click(object sender, ImageClickEventArgs e) 
{ 

        bool type = false; 
    if(hfWasConfirmed.Value == "true") 
{ 
    //If clicks OK button 
    } 
else 
{//If clicks CANCEL button 
} 
} 

在客戶端上:

<asp:HiddenField runat="server" id="hfWasConfirmed" /> 
<asp:Panel runat="server"> 
<script> 
function getConfirmationValue(){ 
    if(confirm('Student has not completed all the steps? Are you sure you want to submit the details?')){ 
     $('#<%=hfWasConfirmed.ClientID%>').val('true') 
    } 
    else{ 
     $('#<%=hfWasConfirmed.ClientID%>').val('false') 
    } 
    return true; 
} 
</script> 
</asp:Panel> 
+0

我認爲hfWasConfirmed.Value將存儲以前的結果,而不是最後一個。不是嗎? – DraggonZ

+0

@DraggonZ我認爲JavaScript函數將在發送表單之前進行評估,因此hfWasConfirmed控件的新值應該在服務器端可用 –

+0

hfWasConfirmed.Value沒有得到(。值不存在) – ARATHY

0

ü可以試試這個也

protected void BtnSubmit_Click(object sender, EventArgs e) 
{ 
    string confirmValue = Request.Form["confirm_value"]; 
    if (grdBudgetMgr.Rows.Count > 0) 
    { 

     if (confirmValue == "Yes") 
     { 


     } 
    } 
}  



<script type="text/javascript"> 
        function Confirm() { 
         var confirm_value = document.createElement("INPUT"); 
         confirm_value.type = "hidden"; 
         confirm_value.name = "confirm_value"; 
         if (confirm("Are you sure Want to submit all Budgeted Requirement ?")) { 
          confirm_value.value = "Yes"; 
         } else { 
          confirm_value.value = "No"; 
         } 
         document.forms[0].appendChild(confirm_value); 
        } 
       </script> 
相關問題