2013-09-21 94 views
0

我有幾個文本框可以查看並更新記錄。在保存或更新&前,請檢查是否有重複記錄。 JavaScript提示彈出'更新',但不'保存'。調試器甚至讀取else塊中的行。我哪裏錯了?C#中的Javascript提醒不起作用

protected void Save_Click(object sender, EventArgs e) 
{ 
    int returnId; 
    returnId = chkDuplicates(value,1);//function to check for any duplicate value 
    if(returnId==0) 
     //save the record 
    else 
    ScriptManager.RegisterStartupScript(this,typeof(Page), "MsgSave", 
             "alert('value exists');", true); 
} 

    protected void Update_Click(object sender, EventArgs e) 
{ 
    int returnId; 
    returnId = chkDuplicates(value,2);//function to check for any duplicate value 
    if(returnId==0) 
     //update the record 
    else 
    ScriptManager.RegisterStartupScript(this,typeof(Page), "MsgUpdate", 
             "alert('value exists');", true); 
} 
+0

您是否在'保存'按鈕後重定向到其他頁面? – karz

+0

這是相同的頁面。 – Ruby

+0

所以點擊'save'不會發生重定向嗎? – karz

回答

0

在按鈕上,添加一個onClientClick來調用JavaScript。如果它通過你,服務器端代碼將被調用。

+0

對不起,但我不明白。 fn。檢查數據庫中的值並取回0或1.另外,我可以在更新單擊中看到警報正常,但它沒有onclientclick。 – Ruby

0

用於顯示JavaScript警告彈出的另一種方式可能是這個

Response.Write("<script>alert(\"Your text here\");</script>"); 
+0

試過了。但沒有運氣。它不顯示任何彈出 – Ruby

+0

嘗試在Save_Click函數調用ScriptManager函數調用中傳遞「MsgSave」作爲第三個參數而不是「Msg」。 –

+0

..........沒有運氣....... – Ruby

0

試試這個...它workd我....

ClientScript.RegisterStartupScript(typeof(Page), "alertMessage", 
"<script type='text/javascript'>alert('value exist');window.location.replace('yourpage.aspx');</script>"); 
+0

剛剛嘗試過。沒有運氣。不顯示任何彈出 – Ruby

+0

驗證您的代碼是否到達'else'部分一次。 – karz

1

請試試這個,它可能有助於

ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), 
    "Msg", "alert('value exists');", true); 
+0

它只適用於更新點擊,但不適用於保存點擊 – Ruby

1

你確定函數chkDuplicates實際上爲Save_Click返回一個非零值電子郵件?

任何方式,這不是實現這種用例的適當方式。最好的方法是將這些方法創建爲Webmethods,並通過使用javascript在按鈕單擊事件上通過ajax調用來調用它們。在存在重複的情況下拋出自定義異常。這會導致您的ajax調用失敗回調。在這個回調顯示watever警報你需要顯示。

這就是你如何優雅地做,而不是在服務器端代碼註冊腳本。這也將節省您的應用程序從發佈後和不必要的頁面重新加載。

+0

同意這不是一種優雅的方式,而是一個新手。返回的值是1,它還讀取else塊中的'scriptmanager ....'行。我想知道,爲什麼同樣的事情可以工作,當我點擊更新,但不保存 – Ruby

+0

您使用相同的名稱註冊您的腳本,這是問題是我相信..然後嘗試一個不同的名稱。 –

+0

如果你想幫助實施它的好方法..檢查此http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ –

1

在這兩個啓動腳本,你的重點是

「消息」

。如果你指望的RegisterStartupScript法「字符串鍵」參數的定義,它說'腳本塊的唯一標識符'。所以它必須是來自特定頁面中其他鍵的唯一鍵。

+0

使感。我改變了參數。但是標誌依然存在。僅在更新 – Ruby

+0

時彈出,並且您單擊以保存時,您已經確保它的代碼正確執行(即調試器進入Save_Click方法)? – sohaiby

0

在這裏,我們顯示警告信息直接

ScriptManager.RegisterStartupScript(this,GetType(),"showalert","alert('Only alert Message');",true); 

在這裏,我們提供了來自JavaScript警告消息

ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true); 

這是兩路在C#中顯示警報消息

+0

他們只與更新點擊工作 – Ruby

+0

「更新點擊」meanns?更新是btn id? – user2561316

+0

update_click是點擊更新按鈕的事件。它的ID是btnUpdate – Ruby

0

請請確保您沒有使用任何更新面板下的保存按鈕。當您執行異步回發時會發生此問題。 我在新的網頁上測試了你的代碼,它工作正常。

代碼在aspx頁面的代碼隱藏文件

protected void Save_Click1(object sender, EventArgs e) 
    { 
     int returnId; 
     returnId = 1; 
     if (returnId == 0) 
     { } 
     else 
      ScriptManager.RegisterStartupScript(this, typeof(Page), "MsgSave", 
               "alert('save value exists');", true); 
    } 
    protected void Update_Click1(object sender, EventArgs e) 
    { 
     int returnId; 
     returnId = 2; 
     if (returnId == 0) 
     { } 
     else 
      ScriptManager.RegisterStartupScript(this, typeof(Page), "MsgUpdate", 
               "alert('update value exists');", true); 
    } 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Button ID="Save" runat="server" Text="Save" onclick="Save_Click1" /> 
     <asp:Button ID="Update" runat="server" Text="Update" onclick="Update_Click1" /></div> 
    </form> 
</body> 
</html> 

代碼可這會幫助你。