您面临的问题是因为您正在重新加载页面,因此您注册的脚本会丢失。
我在这种情况下使用的一种方法涉及使用jQuery和jQuery UI Dialog:
在您的页面中,放置一个将作为消息容器的 div。它最初是隐藏的,将在完成数据库请求后显示:
<div id="modal" title="Alert" style="display: none;">
</div>
编写一个将显示对话框的 javascript 函数:
function showConfirmation(text){
$("#modal").html(text).dialog({
modal: true, // show the dialog as modal
buttons: {
Ok: function() {
location.reload();
}
}, // add a button that refreshes the page when clicked
closeOnEscape: false, // avoid the dialog close when ESC is pressed
dialogClass: 'no-close' // adds a CSS class that hides the default close button
});
}
该dialog
函数负责显示对话框,使用 jQuery UI 库。该buttons
参数显示一个按钮,按下时会刷新页面。
您所要做的就是使用以下RegisterStartupScript
方法注册对话脚本:
ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "showConfirmation('Edited Successfully !');", true);
如果您不使用 jQuery 和/或 jQuery UI,您所要做的就是在 head 标签中添加引用。如果您不想使用CDN,请将文件下载到您的本地站点。
<script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
您可以看到这个小提琴的客户端行为的工作示例