gridview中的分页警报消息

时间:2014-02-25 12:21:39

标签: c# javascript asp.net gridview

我想在用户尝试在gridview中进行分页时显示警告。我正在使用此脚本,但即使在页面中单击了页面中的任何按钮,也会弹出警报。我只需要在点击分页时发出警报。

<script type="text/javascript">
    function closeEditorWarning() {
        return 'It looks like you have been editing something -- if you leave before submitting your changes will be lost.'
    }

    window.onbeforeunload = closeEditorWarning
</script>

真的很感激任何帮助。

2 个答案:

答案 0 :(得分:0)

gridview_pageindexchanging()事件中编写一个java脚本警报来执行此操作:

您可以使用脚本管理器执行此操作,如下所示:

You have double script tags. Add the script tags yourself:

protected void grid_pageindexchanging(object sender, GridViewPageEventArgs e) {
   string script = "<script type=\"text/javascript\">alert('abc');</script>";
   ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
}

让方法添加它:

protected void grid_pageindexchanging(object sender, GridViewPageEventArgs e) {
   string script = "alert('abc');";
   ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
} 

希望这会有所帮助..

<强>更新

尝试包含以下内容:

using  System.Web.Script.Serialization 

已更新2:

你现在可以试试吗:

string script = "alert('its working now!')";
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "its working",script,true);

答案 1 :(得分:0)

如果jQuery是一个选项,请在寻呼机上设置CSS类,并将脚本与该类的click事件相关联。

未经测试但可能正确的方向:

$('.myClass').click(function () { closeEditorWarning; });