c#
  • javascript
  • asp.net
  • handler
  • scriptmanager
  • 2011-10-11 37 views -1 likes 
    -1

    我有一箇中繼器象下面這樣:如何使用ScriptManager.RegisterStartUpScript在handler.ashx文件中調用JavaScript函數?

        <asp:Repeater ID="Repeater1" runat="server"> 
             <ItemTemplate> 
              <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("FilePath","~/HandlerForRepeater.ashx?path={0}") %>'><%# Eval("FileName")%></asp:HyperLink> 
              <br /> 
              <asp:Label ID="Label3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FileCreationDate", "{0:tt h:m:s - yyyy/MM/dd}") %>'></asp:Label> 
              <hr /> 
             </ItemTemplate> 
            </asp:Repeater> 
    

    我有一個HandlerForRepeater.ashx爲另存爲對話框如下圖所示:

    using System; 
        using System.Collections.Generic; 
        using System.Linq; 
        using System.Web; 
    
        namespace FileExplorer 
        { 
         /// <summary> 
         /// Summary description for HandlerForRepeater 
         /// </summary> 
         public class HandlerForRepeater : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
         { 
    
          private HttpContext _context; 
          private HttpContext Context 
          { 
           get 
           { 
            return _context; 
           } 
           set 
           { 
            _context = value; 
           } 
          } 
    
          public void ProcessRequest(HttpContext context) 
          { 
           Context = context; 
           string filePath = context.Request.QueryString["path"]; 
           filePath = context.Server.MapPath(filePath); 
    
           if (filePath == null) 
           { 
            return; 
           } 
    
           System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath); 
           System.IO.BinaryReader br = new System.IO.BinaryReader(streamReader.BaseStream); 
    
           byte[] bytes = new byte[streamReader.BaseStream.Length]; 
    
           br.Read(bytes, 0, (int)streamReader.BaseStream.Length); 
    
           if (bytes == null) 
           { 
            return; 
           } 
    
           streamReader.Close(); 
           br.Close(); 
           string fileName = System.IO.Path.GetFileName(filePath); 
           string MimeType = GetMimeType(fileName); 
           string extension = System.IO.Path.GetExtension(filePath); 
           char[] extension_ar = extension.ToCharArray(); 
           string extension_Without_dot = string.Empty; 
           for (int i = 1; i < extension_ar.Length; i++) 
           { 
            extension_Without_dot += extension_ar[i]; 
           } 
    
           //if (extension == ".jpg") 
           //{ // Handle *.jpg and 
           // WriteFile(bytes, fileName, "image/jpeg jpeg jpg jpe", context.Response); 
           //} 
           //else if (extension == ".gif") 
           //{// Handle *.gif 
           // WriteFile(bytes, fileName, "image/gif gif", context.Response); 
           //} 
    
           if (HttpContext.Current.Session["User_ID"] != null) 
           { 
            WriteFile(bytes, fileName, MimeType + " " + extension_Without_dot, context.Response); 
           } 
           else 
           { 
    System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "MyMethod", "alert('You Can Not Download - pzl Login First');", true); 
           } 
          } 
    
          private void WriteFile(byte[] content, string fileName, string contentType, HttpResponse response) 
          { 
           response.Buffer = true; 
           response.Clear(); 
           response.ContentType = contentType; 
    
           response.AddHeader("content-disposition", "attachment; filename=" + fileName); 
    
           response.BinaryWrite(content); 
           response.Flush(); 
           response.End(); 
          } 
    
          private string GetMimeType(string fileName) 
          { 
           string mimeType = "application/unknown"; 
           string ext = System.IO.Path.GetExtension(fileName).ToLower(); 
           Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); 
           if (regKey != null && regKey.GetValue("Content Type") != null) 
            mimeType = regKey.GetValue("Content Type").ToString(); 
    
            return mimeType; 
           } 
    
           public bool IsReusable 
           { 
            get 
            { 
             return false; 
            } 
           } 
          } 
         } 
    

    每一件事情是確定的這個處理程序,但我想說明的如果Session [「User_ID」]爲空,則向我的用戶發出警告!
    所以我的問題是在下面一行:

    System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "MyMethod", "MyMethod();", true); 
    

    這行有錯誤在這個哈德勒!
    我該如何在HandlerForRepeater.ashx中調用這些JavaScript方法?

    在此先感謝

    回答

    1

    你應該做的是從頁的Repeater控件是不是處理程序。在後面的代碼的頁面轉發:

    if(HttpContext.Current.Session["User_ID"] != null) 
    { 
        Response.Redirect("~/HandlerForRepeater.ashx?path={FilePath}"); 
    } 
    else 
    { 
        ClientScript.RegisterStartupScript(this.GetType(), "MyMethod", "MyMethod();", true); 
    } 
    

    你可以輸出從該頁面的文件,但儘量堅持當前的例子儘可能的。

    +0

    感謝回答/但我應該使用哪個服務器端事件? – MoonLight

    +0

    我會將超鏈接更改爲鏈接按鈕並使用它的單擊事件。 – Justin

    +0

    感謝兄弟 - 你救了我 - 我在LinkBut​​ton中使用了CommandName! – MoonLight

    相關問題