2015-05-07 37 views
0

如何在ScriptManager.RegisterClientScriptBlock中顯示catch錯誤消息? 下面是我的按鈕點擊event.like我們可以通過使用ex.message獲得標籤中的錯誤消息,所以如何使用此警報彈出消息框?如何在ScriptManager.RegisterClientScriptBlock中顯示catch錯誤消息

public void IssueDelete() 
    { 
    try 
    { 
     //open the db connection if it is closed... 
     if (connection.State == ConnectionState.Closed) 
      connection.Open(); 
     int issueId = Convert.ToInt32(ddlComplaintDelete.SelectedValue); 
     command = new SqlCommand(); 
     command.CommandText = "sp_IssueDelete"; 
     command.CommandType = CommandType.StoredProcedure; 
     command.Parameters.AddWithValue("@registrationId", lblUploadRegistrationId.Text); 
     command.Parameters.AddWithValue("@issueId", issueId); 
     command.Connection = connection; 
     command.ExecuteNonQuery(); 
     ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Issue Deleted Succesfully');", true); 
    } 
    catch (Exception ex) 
    { 
     ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Cannot Delete');", true); 
    } 
    finally //Close db Connection if it is open.... 
    { 
     if (connection.State == ConnectionState.Open) 
     connection.Close(); 
    } 
    } 
protected void btnComplaintDelete_Click(object sender, EventArgs e) 
     { 
     IssueDelete();    
     } 
+1

你在哪裏發現C#代碼或JavaScript錯誤? – Adil

+0

沒有收到任何錯誤,但消息框沒有發生變化。 – asma

+0

你有RegisterClientScriptBlock語句的地方嗎? – Adil

回答

1

您可能打破了您註冊的警報的JavaScript。您可能會獲得單引號或雙引號,即也可能會破壞JavaScript。正確連接ex.Message,並從異常消息中刪除任何單/雙引號。

try 
{ 
    //Your code 
    //throw new Exception("he'\""); 
} 
catch(Exception ex) 
{ 
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Cannot Delete Exception Message: " + ex.Message.Replace("'", "").Replace("\"", "") + "');", true); 
} 
finally //Close db Connection if it is open.... 
{ 
    if (connection.State == ConnectionState.Open) 
     connection.Close(); 
} 
+0

我現在知道了...非常非常thanq先生阿迪爾先生.. – asma

+0

不客氣@asma,感謝您的耐心,因爲我問你這麼多問題 – Adil

+0

haha​​ha ..沒問題先生,其需要我.. – asma