0

我几乎没有要查看和更新​​记录的文本框。这是我在保存或更新之前检查任何重复记录的代码,如果有任何重复记录并提醒用户。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);
 }
4

8 回答 8

1

在您的两个启动脚本中,您的密钥都是

“消息”

如果您在 RegisterStartupScript 方法中查找“字符串键”参数的定义,它会显示“脚本块的唯一标识符”。因此它必须与特定页面中的其他键唯一。

于 2013-09-21T09:07:10.007 回答
1

请试试这个,它可能会有所帮助

   ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
     "Msg", "alert('value exists');", true);
于 2013-09-21T08:57:05.447 回答
1

您是否确定函数 chkDuplicates 实际上为 Save_Click 用例返回了一个非零值?

任何方式这都不是实现此类用例的适当方式。最好的方法是将这些方法创建为 Web 方法,并使用 javascript 对按钮单击事件进行 ajax 调用来调用它们。如果存在重复项,则抛出自定义异常。这将导致您的 ajax 调用出现故障回调。在此回调显示水警报您需要显示。

这就是你如何优雅地做到这一点,而不是在服务器端代码中注册脚本。这还将使您的应用程序免于回发和不必要的页面重新加载。

于 2013-09-21T09:01:37.143 回答
0

这里我们直接显示警报消息

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

在这里,我们显示来自 javascript 的警报消息

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

这些是在 c# 中显示警报消息的两种方式

于 2013-09-21T09:20:09.880 回答
0

显示 javascript 警报弹出窗口的另一种方法可能是这样

Response.Write("<script>alert(\"Your text here\");</script>");
于 2013-09-21T08:51:37.597 回答
0

试试这个……它对我有用……

    ClientScript.RegisterStartupScript(typeof(Page), "alertMessage",
"<script type='text/javascript'>alert('value exist');window.location.replace('yourpage.aspx');</script>");
于 2013-09-21T08:55:42.387 回答
0

请确保您没有使用任何更新面板下的保存按钮。当您执行异步回发时会出现此问题。我在新网页中测试了您的代码,它工作正常。

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

可能这会对你有所帮助。

于 2013-09-24T12:31:10.910 回答
0

在按钮上,添加一个 onClientClick 来调用 JavaScript。如果它然后通过你,服务器端代码将被调用。

于 2013-09-21T08:47:49.293 回答