Click here to Skip to main content
11,639,765 members (59,958 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
I am uploading pdf files to my asp.net project. I then display the uploaded files in a gridview and have options to download, delete and view the files. How do I make the modalpopupextender know the path of the uploaded file and display it? I want the modalpopup to display once I have clicked on the 'view' link button.

I am not using a SqlDataSource. Here's my code so far:
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
                List<ListItem> files = new List<ListItem>();
                foreach (string filePath in filePaths)
                {
                    files.Add(new ListItem(Path.GetFileName(filePath), filePath));
                }
                GridView1.DataSource = files;
                GridView1.DataBind();
            }
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
                try
                {
                    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
                catch (Exception ex)
                {
                    Label1.Text = "ERROR: " + ex.Message.ToString();
                }
            else
            {
                Label1.Text = "You have not specified a file.";
            }
        }
 
        protected void DownloadFile(object sender, EventArgs e)
        {
            string filePath = (sender as LinkButton).CommandArgument;
            Response.ContentType = ContentType;
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
            Response.WriteFile(filePath);
            Response.End();
        }
 
        protected void DeleteFile(object sender, EventArgs e)
        {
            string filePath = (sender as LinkButton).CommandArgument;
            File.Delete(filePath);
            Response.Redirect(Request.Url.AbsoluteUri);
        }
 
        protected void ViewFile(object sender, EventArgs e)
        {
            
        }


Here is my design page so far:
<style type="text/css">
 

        .ModalPopupBG
        {
            background-color: #666699;
            filter: alpha(opacity=50);
            opacity: 0.7;
        }
 
        .popupControl
        {
            background-color:#AAD4FF;
            position:absolute;
            visibility:hidden;
            border-style:solid;
            border-color:Black;
            border-width: 2px;
        }
    </style>
</head>
 
<body>
    <form id="form1" runat="server">
    <div>
 
        <h2>
 
        <asp:Label ID="Label2" runat="server"
            style="font-size: larger; font-weight: 700" Text="Document Management System"></asp:Label>
        </h2>
        <br />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="Label3" runat="server" style="font-weight: 700"
            Text="Uploading Files"></asp:Label>
        <br />
        <br />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:FileUpload ID="FileUpload1" runat="server" />
       
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />
&nbsp;
        
        <asp:Label ID="Label4" runat="server"></asp:Label>
        
        <asp:Label ID="Label1" runat="server"></asp:Label>
        
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            EmptyDataText = "No files uploaded" Height="183px"
            style="text-align: center; margin-left: 20px" Width="601px" >
        <Columns>
        <asp:BoundField DataField="Text" HeaderText="File Name" />
            <asp:TemplateField HeaderText="Reference No.">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Description">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Category">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
 
                <asp:LinkButton ID="lnkDownload" Text = "Download" CommandArgument = '<%# Eval("Value") %>' runat="server" OnClick = "DownloadFile"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID = "lnkDelete" Text = "Delete" CommandArgument = '<%# Eval("Value") %>' runat = "server" OnClick = "DeleteFile" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="title" runat="server" NavigateUrl="~/red.htm">HyperLink</asp:HyperLink>
                <asp:LinkButton ID = "lnkView" Text = "View" CommandArgument = '<%# Eval("Value") %>' runat = "server" OnClick = "ViewFile"    />
                <asp:Panel ID="pnlUpload" runat="server" Height="406px" Width="782px">
            <asp:Button ID="closeBtn" runat="server" Text="Close" />
        </asp:Panel>
                <asp:ModalPopupExtender ID="lnkView_ModalPopupExtender" runat="server"
                    DynamicServicePath="" Enabled="True" TargetControlID="lnkView" PopupControlID="pnlUpload" BackgroundCssClass="ModalPopupBG" OkControlID="closeBtn">
                </asp:ModalPopupExtender>
 
            </ItemTemplate>
        </asp:TemplateField>
            <asp:TemplateField></asp:TemplateField>
    </Columns>
    </asp:GridView>
       
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>

Please help. Thank you in advance
Posted 6-Jan-13 19:44pm
Edited 6-Jan-13 23:20pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I haven't been able to use the modalpopupextender and panel. I removed the controls from my design page. However, I added code to the ViewFile click event:
protected void ViewFile(object sender, EventArgs e)
        {
            LinkButton lnk = (LinkButton)sender;
            if (lnk != null)
            {
                string filePath = (sender as LinkButton).CommandArgument;
                Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFileName(filePath) + ".pdf");
                Response.WriteFile(Server.MapPath("~/Uploads/" + Path.GetFileName(filePath)));
                Response.End();
            }
        }

When I click on the View link button, the .pdf file appears. I still would like to view it in a modalpopupextender or even an iframe.

I still welcome suggestions. Smile | :)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Suvendu Shekhar Giri 330
1 CPallini 177
2 F-ES Sitecore 170
3 sasanka sekhar panda 155
4 jyo.net 142
0 OriginalGriff 9,081
1 Sergey Alexandrovich Kryukov 8,847
2 Mika Wendelius 7,027
3 Suvendu Shekhar Giri 2,650
4 F-ES Sitecore 2,558


Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100