在Data表从sql获取数据后确认Box

时间:2014-01-06 08:49:58

标签: c# javascript asp.net .net

 function alertbox() {
    var mode = document.getElementById('<%= hdnMode.ClientID %>').value;
    if (mode == "EDIT")
        return false;

    if (confirm("the same data is present against that ID ?") == true) {
        document.getElementById('<%= hdnYesNo.ClientID %>').value = "COPY";
    }
    else {
        document.getElementById('<%= hdnYesNo.ClientID %>').value = "CANCEL";
    }
 }

从sql和

检索数据后,应出现上述确认消息
Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction",
"MyFunction()",true);

如何从代码隐藏中使用它,如果是,那么如何根据值获取返回值 复制并取消

1 个答案:

答案 0 :(得分:0)

我将尝试提供一个我刚刚创建的测试应用程序示例。

首先,我使用ScriptManager应用我希望在网页上出现的所有javascript文件,如下所示:

<body>
    <form id="form1" runat="server">
    <div>        
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Scripts>
                <asp:ScriptReference Path="~/JS/tester.js" />
            </Scripts>
        </asp:ScriptManager>                
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
                    Text="Call database" />                
    </div>
    </form>
</body>

<Scripts>标记中添加更多JS文件。这将确保在您加载网页时添加您的javascript文件。

我的tester.js中的代码是:

function alertbox(data) {
    alert("Completed the database operation with following data = " + data);
}

现在来看你的代码背后的场景,我在我的示例数据中有,我在网页上创建了一个按钮,可以执行一些数据库操作,一旦完成,它将提醒用户有关sql更新的信息。

按钮事件处理程序如下:

protected void Button1_Click(object sender, EventArgs e)
{
     Thread.Sleep(2000);
     int sqlReturnValue = ExecuteTheQuery();
     ScriptManager.RegisterStartupScript(this, typeof(string), "Database Completed", "alertbox(" + sqlReturnValue  + ");", true);
}

现在这将调用Javascript函数警告框。

(注意:这只是你如何实现预期的一个小例子)

<强>更新

使用ClientScript也可以实现同样的目的。 我所做的是,添加一个脚本标记:

<head runat="server">
    <title>Test Page</title>
    <script src="JS/tester.js" type="text/javascript"></script>
</head>

在按钮后面的代码中单击:

protected void Button1_Click(object sender, EventArgs e)
{
    Thread.Sleep(2000);
    ClientScript.RegisterStartupScript(this.GetType(), "Database Completed", "alertbox(23);", true);
}

要了解ClientScript和ScriptManager,请检查此question