解决AJAX中使用UpdatePanel后再用Response.Write();等无法弹出对话框问题(转)

在AJAX支持的网站中使用想使用Response.Wrie(“”); 或 Page. RegisterStartupScript (); 弹出一些提示对话框,没有效果。有如下两种解决方法:

一、System.Web.UI.ScriptManager.RegisterStartupScript来替代Page.ClientScript.RegisterStartupScript
    函数原型:System.Web.UI.ScriptManager.RegisterStartupScript(Contrl control, Type type,string key,string script, bool addScriptTags);
    参数:control要要注册此段javascript语句的控件ID,如下面示例的按钮ID btnUnReporttype一般直接用this.GetType()即可
    key为要执行的javascirpt语句起的名字,可以随便起,类似控件的name属性script javascript语句addScriptTags为true时,前边的script参数可以不用再写javascript标签;为false,则需自己为script参数添加<script language=’javascript’></script>标签。
注:要多传一个Control参数,如果这个control在UpdatePanel里,则运行脚步,如果这个control不在UpdatePanel则不允许这段脚步。Eg.System.Web.UI.ScriptManager.RegisterStartupScript(btnUnReport, this.GetType(), "unReport", "alert('撤销上报操作失败,请勾选要上交的课题!');window.close();", true);

二、注册DataItem方法

示例:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs" Inherits="WebForm1" %>
<!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">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <script type="text/javascript">
         Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(
             function(sender, e)
            ...{
                 var dataItem = e.get_dataItems()["<%= this.UpdatePanel1.ClientID %>"]();
                 alert(dataItem.Name);
             });
    </script>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Button ID="test" runat="server" Text="test" OnClick="test_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>

    </form>
</body>
</html>

CODE:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Web.Script.Serialization;

    public partial class WebForm1 : System.Web.UI.Page
    ...{
        protected void Page_Load(object sender, EventArgs e)
        ...{

         }

        protected void test_Click(object sender, EventArgs e)
        ...{
             JavaScriptSerializer serializer = new JavaScriptSerializer();
             ScriptManager.GetCurrent(this.Page).RegisterDataItem(this.UpdatePanel1, "var _f = function(){alert('Hello World!');}; _f;", true);

         }

     }

三、使用Javascript的非模态对话框弹出提示Eg.Page.RegisterStartupScript("alert", "<script language='javascript'>window.showModelessDialog(\"javascript:alert('撤销上报操作失败,请勾选要上交的课题!');window.close();\",\"\",\"status:no;resizable:no;help:no;dialogHeight:height:30px;dialogHeight:40px;\")</script>");

四、用下面的方法代替Response.Write(): (本人用VS2005通过验证)
System.Web.UI.ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "alert('Hello Word!')", true);

五、也可用AjaxControlToolkit的ConfirmButtonExtender控件。

六、

1.添加引用
using MagicAjax.UI;
2.response.write改成類似以下語句
string messageContent = "放js語句";
MagicAjax.AjaxCallHelper.Write("alert('" + messageContent + "');");

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cryeyes/archive/2009/07/31/4397909.aspx

posted @ 2009-08-05 20:28  Just for Fun  阅读(551)  评论(0编辑  收藏  举报