1

我有这个页面,您可以在其中“编辑”用户的数据......我想发送一条消息,如“编辑成功”,并使用新内容(DropDownLists 和 TextBoxes)更新页面。

我将其用于消息:

ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('Edited Successfully !');", true);

为了更新页面,我尝试了: Response.Redirect(Request.RawURl)甚至一个简单的也Response.Redirect(~/page.aspx)没有用......

如果我尝试这种方式,它确实会更新页面,但它不会显示警报... ;\ 所有这些数据(填充 DropDownLists、文本框等)都在 Page_Load 上调用。我在发送消息后尝试调用相同的方法,但是它没有更新 =\

任何想法?

4

1 回答 1

1

您面临的问题是因为您正在重新加载页面,因此您注册的脚本会丢失。

我在这种情况下使用的一种方法涉及使用jQueryjQuery 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">

您可以看到这个小提琴的客户端行为的工作示例

于 2013-03-08T21:08:41.253 回答