2011-01-20 77 views
1

裏面我boxLang_OnSelectedIndexChanged()事件中執行代碼的JavaScript確認框的背後我得到這個: -在下拉框中的選擇指數變化事件觸發

if (txtbox1.Text != "" && txtbox2.Text != "") 
     { 
      ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.');", true); 
      txtbox1.Text = ""; 
      txtbox2.Text = ""; 
     } 

現在正在發生的事情是。當我從下拉列表中選擇一個不同的項目時,此確認框顯示出來,但是它首先清除了文本框的內容,然後出現該確認框。我想要的是文本框的內容應該只在單擊確定時清除。

我在做什麼錯?請幫忙。謝謝。

編輯 @jgauffin **我輸入回報,你有。現在發生的事情是,它只是馬上清除文本框,這次甚至不顯示確認框!現在怎麼了?請reply..thnx **

編輯2 我試過如下: -

裏面我BindDropDown()方法,我說: -

dropdownbox1.Attributes.Add("onChange","DisplayConfirmation()"); 

然後在我的aspx頁面我已經添加了以下腳本,如下所示: -

<script type="css/javascript"> 
function DisplayConfirmation() { 
    if (confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.')) { 
    __doPostback('dropdownbox1',''); 
    } 
} 

</script> 

此函數沒有被調用whe n下拉選擇的索引被更改。怎麼了?

編輯3: - 好,我改變了一行代碼,如下所示: -

dropdownbox1.Attributes.Add("onchange", "javascript:return DisplayConfirmation()"); 

仍然不會工作。我是JavaScript新手,並且都遇到了問題。幫助將不勝感激。

+0

類型化編輯器通過mistake..have editted it..sorry – Serenity 2011-01-20 13:34:07

回答

3
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "return confirm('Changing the language will clear the text in the textboxes. Click OK to proceed.');", true); 

回報是需要打破取消該過程。

+0

我在哪裏需要鍵入回報? – Serenity 2011-01-20 13:37:12

1

這不起作用。您將發回服務器,然後註冊javascript確認代碼,然後清除內容。只有當頁面重新加載時,javascript確認框纔會出現,屆時您已經清除了文本框的值。這就像你在混淆客戶端和服務器端編程。像JavaScript這樣的客戶端代碼只會在服務器完成它的事情並加載頁面時執行。

你需要的是這個答案,大概是:

DropdownList autoposback after client confirmation

相關問題