0

i have a repeater like below :

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

i have an HandlerForRepeater.ashx for save as dialog like below :

 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;
                    }
                }
            }
        }

every thing is ok about this handler , but i want to show an alert to my users if Session["User_ID"] is Null!
so my problem is in below line :

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

and this line has error in this hadler!
how can i call such these javascript methods in HandlerForRepeater.ashx?

thanks in advance

1 Answer 1

1

You should be doing that from the page your repeater control is on not the handler. In the code behind for the page with repeater:

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

You can just output the file from that page but trying to stick to your current example as much as possible.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for answer / but which server - side event should i use?
I would change the hyperlink to a link button and use it's click event.
thanks bro - you saved me - i used CommandName in LinkButton!
at last i just want to know was that possible to do that job in handler file ?
Short answer - no. The handler is not a page, it will not register script on the calling page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.