in

SDT Community Server

SDT Forums, Blogs, Photos server.

wego

October 2008 - Posts

  • UpdatePanel & UpdateProgress & Download File 的相关问题

          程序中有时会因要执行业务比较复杂, 运行时间长的一系统操作, 需要显示如 "执行中 .." 等用户友好提示,  可用ASP.NET AJAX 的 UpdatePanel & UpdateProgress 控件完成. 但若执行结果需要下载文件时, 则会出现执行完成但文件却下载不了的情况. 因为 UpdateProgress 是异步进行中.  解决方法可以建立一个 'DownloadFile.aspx' 页面作辅助, 请见如下主要代码片断  

    -------  Data.aspx ----------- 

    <asp:ScriptManager ID="ScriptManager" runat="server" /> 

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate> 

                   <asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="2" DynamicLayout="true">
                                <ProgressTemplate>
                                    <img src='<%=CommonUrl.Images %>wait_clock.gif' />
                                    <span id='spanProgressNote'></span>
                                </ProgressTemplate>
                  </asp:UpdateProgress>

             </ContentTemplate>
    </asp:UpdatePanel>  

     <script language="javascript">

            Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(initializeRequestHandler);

            function initializeRequestHandler(sender, e) {
                var postBackElementId = e.get_postBackElement().id;

                switch (postBackElementId) {
                    case '<%= btnSearch.ClientID %>':
                        spanProgressNote.innerHTML = 'Loading ..';
                        break;

                    case '<%= imgExport.ClientID %>':
                        spanProgressNote.innerHTML = 'Exporting ..';
                        break;
                  }                      

             }       

    </script>

    -------  Data.cs ----------- 

    protected void imgExport_Click(object sender, ImageClickEventArgs e)
     {
            ...

           // SimpleWebUtils.DownloadFile(Response, filePath, Path.GetFileName(filePath));     // 不能直接在当前页下载文件 

            ScriptManager.RegisterStartupScript(this, this.GetType(), "RegisterStartupScript"
                                                    , String.Format("window.navigate('../Control/'DownloadFile.aspx?path={0}');", Server.UrlEncode(filePath)), true);


            //ScriptManager.RegisterStartupScript(this, this.GetType(), "RegisterStartupScript"
            //                                       , String.Format("window.navigate('{0}');", fileUrl), true);      // 用这种方式可以不用 ''DownloadFile.aspx' 页面但下载文件直接打开是用网页方式而不是实际文件方式 (如: Excel)

     }

    -------  DownloadFile.cs -----------

     protected void Page_Load(object sender, EventArgs e)
     {
            string path = Request.QueryString["path"];
            SimpleWebUtils.DownloadFile(Response, path, Path.GetFileName(path));
            ScriptManager.RegisterStartupScript(this, this.GetType(), "RegisterStartupScript", "window.close();", true);                                                
     }

  • 当心 Oracle Procedure 中的 IN 参数也会被你无意中修改了它的值

    在 Oracle Procedure 中使用 IN 参数似乎很好地对该参数起到保护作用, 其实不然! 请看如下 Code

    -- Spec Part 

    PACKAGE TEST
    IS
       g1   NUMBER;

       PROCEDURE proc1;

       PROCEDURE proc2 (p1 IN NUMBER);
    END;        

    -- Body Part

     PACKAGE BODY TEST
    IS
       PROCEDURE proc1
       IS
       BEGIN
          g1 := 1;
          proc2 (g1);
          DBMS_OUTPUT.put_line ('g1=' || g1);
       END;

       PROCEDURE proc2 (p1 IN NUMBER)
       IS
          c1   CONSTANT NUMBER := p1;
       BEGIN
          --p1 := p1 + 1;  这句是编译不通过的, 似乎已对 IN 参数保护了.
          DBMS_OUTPUT.put_line ('p1=' || p1);
          DBMS_OUTPUT.put_line ('c1=' || c1);
          g1 := g1 + 1;  -- 这句间接地改变了 IN 参数值!
          DBMS_OUTPUT.put_line ('after p1=' || p1);   -- 已经被改了!
          DBMS_OUTPUT.put_line ('after c1=' || c1); 
       END;
    END;

    运行 proc1 后输出:

    p1=1
    c1=1
    after p1=2
    after c1=1
    g1=2

    总结:

    1) 若要确保过程内任意地方都能访问到 IN 参数的初始值, 需要使用附加常量

    2) 尽可能少用全局变量 (风险很大)

    3) 其它非 Oracle 数据库也可能出现该情况.

     

    Posted Oct 08 2008, 03:29 PM by wego with no comments
    Filed under:
Copyright SDT, 2006-2009. All rights reserved.