我怎么能调用javascript函数与ScriptManager.RegisterStartUpScript内部处理程序

本文关键字:RegisterStartUpScript 内部 处理 程序 ScriptManager 怎么能 调用 javascript 函数 | 更新日期: 2023-09-27 18:11:02

我有一个像下面这样的重复器:

                    <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。

保存为对话框
 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;
                    }
                }
            }
        }

关于这个处理程序的一切都是好的,但我想向我的用户显示一个警报,如果会话["User_ID"]是空的!
所以我的问题在下面一行:

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

和这一行在这个处理程序中有错误!
我如何在HandlerForRepeater.ashx中调用这些javascript方法?

thanks in advance

我怎么能调用javascript函数与ScriptManager.RegisterStartUpScript内部处理程序

您应该从您的重复器控件的页面上做,而不是处理程序。在带有repeater的页面后面的代码中:

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

您可以直接从该页输出文件,但尽量坚持当前的示例

相关文章:
  • 没有找到相关文章