簡體   English   中英

C#中的Javascript警報不起作用

[英]Javascript alert in C# not working

我只有幾個文本框可以查看和更新​​記錄。 這是我的代碼,用於在保存或更新之前檢查是否有重復記錄,並在有任何記錄時提醒用戶。 javascript警報針對“更新”彈出,而不針對“保存”彈出。 調試器甚至讀取else塊中的行。 我要去哪里錯了?

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);
 }

請嘗試這個,可能會有所幫助

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

您是否確定chkDuplicates函數實際上為Save_Click用例返回了一個非零值?

任何方式都不是實現此類用例的適當方式。 最好的方法是將這些方法創建為Web方法,並使用javascript通過按鈕單擊事件上的ajax調用來調用它們。 如果重復項存在,則拋出一個自定義異常。 這將導致ajax調用發生故障回調。 在此回叫顯示水警報中,您需要顯示。

這是您優雅地執行操作的方法,並且不會在服務器端代碼中注冊腳本。 這還將使您的應用程序免於回發和不必要的頁面重新加載。

在兩個啟動腳本中,您的密鑰是

“消息”

如果您在RegisterStartupScript方法中查找“字符串鍵”參數的定義,則會顯示“ 腳本塊唯一標識符 ”。 因此它必須與特定頁面中的其他鍵唯一。

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

顯示javascript警報彈出窗口的另一種方法可能是這樣

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

試試這個...對我有用....

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

在這里,我們直接顯示警報消息

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

在這里,我們顯示了來自javascript的警報消息

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

這是在C#中顯示警報消息的兩種方式

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

ASPX頁面上的代碼

<%@ 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>

代碼隱藏文件中的代碼

 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);
    }

可能會對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM