2011-08-16 68 views
1

我想添加一個提醒框,以便用戶可以選擇是或否或確定並取消,但它不能正常工作。我必須做一個數據庫檢查,這是在C徹底完成而不是隻需將該功能鏈接到按鈕點擊的事件即可。這是我第一次嘗試這樣做。我正在使用visual studio 2010.我不確定我的代碼是否正確。任何人都可以請引導我,如果我錯了。Javascript提醒問題

  private void AlertWithConfirmation() 
      { 
      Response.Write("<script language='javascript'>"); 
      Response.Write("function onsub() "); 
      Response.Write("{ "); 
      Response.Write("return confirm(\"Are you sure?\")"); 
      Response.Write("} "); 
      Response.Write("</script>"); 
      } 

這是我完全C#代碼:

protected void Import_Click(object sender, EventArgs e) 
    { 
     if (!Validation.ValidateDateFormat(dateField.Text)) 
     { 
      errorMessageLabel.Text = "Invalid Date Format, for example: 1/1/2011 should be 01/01/2011"; 
     } 
     else 
     { 
      //Validation to check if data is already imported 
      if (BusinessLayerHandler.isImported(dateField.Text) == false) 
      { 
       try 
       { 
        if (BusinessLayerHandler.isInProgress(dateField.Text)== true) 
        { 
         AlertWithConfirmation(); 
        } 
       } 
       catch 
       { 
        //catch error 
       } 
      } 
      else if (BusinessLayerHandler.isImported(dateField.Text) == true) 
      { 
       Alert("That date was already imported"); 
      } 
     } 
+0

你錯了,這個代碼是毫無意義的。你的目標究竟是什麼?如果用戶單擊「取消」按鈕,您試圖防​​止什麼? –

+0

您確認後是否嘗試過分號? – ibram

+0

您正在向頁面寫入函數,但觸發函數的位置在哪裏?還有其他的方式,我認爲這應該完成,但考慮到你的例子,好像你希望onsub方法在頁面重新加載時觸發,在這種情況下,你需要將它附加到文件onload eveent。 –

回答

1

你的代碼是完全正確的。只有語法錯誤。只需刪除「\」,然後在行中雙引號開始 - > Response.Write(「return confirm(\」Are you sure?\「)」);
替換爲這一行
Response.Write(「return confirm(」Are you sure?\「)」);

0

我看不到你在哪裏調用函數。爲什麼不讓函數總是在頁面上動態添加函數調用到後面代碼中的按鈕?

喜歡的東西:

button.Attributes.Add("onclick", "onsub();"); 
+0

@ giovanni galbo ....我在頁面加載時調用它:protected void Page_Load(object sender,EventArgs e) { AlertWithConfirmation(); } – mikespiteri

+0

這不是調用JavaScript函數......它調用編寫javascript函數的代碼。 –

+0

對不起,我不確定。這是JavaScript的功能? private void AlertWithConfirmation(){Response.Write(「」); } – mikespiteri