Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pdf Viewer refreshes during page index change of gridview on asp.net page

Posted on 2015-02-09
4
Medium Priority
?
202 Views
Last Modified: 2015-02-16
I have a gridview on an asp.net page with two link buttons - one for viewing a pdf and one for downloading.   When  you click View a pdf viewer is displayed in a literal control.  This works fine.  But I have paging enabled for my gridview and when I click the paging links the pdf display refreshes, meaning it will reload the last pdf viewed.  I'm guessing it has something to do with postback but can't figure out a solution.
<div id="wpaLinks" style="height: 118px;">
                <asp:UpdatePanel ID="UpdatePanel2" runat="server">                         
                    <ContentTemplate>
                        <asp:GridView ID="gvWPAs" runat="server" AutoGenerateColumns="False" AllowPaging="true" PageSize="3" CssClass="Grid" AlternatingRowStyle-CssClass="alt"  
                            PagerStyle-CssClass="pgr" EmptyDataText="No Records Found" OnPageIndexChanging="gvWPAs_PageIndexChanging">  <%--OnRowDataBound="gvWPAs_RowDataBound"--%>
                                <AlternatingRowStyle CssClass="alt" />
                                <Columns>
                                    <asp:BoundField DataField="fileDescription" HeaderText="File Description" />
                                    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                                        <ItemTemplate>
                                            <asp:LinkButton ID="lnkView" runat="server" Text="View" OnClick="View" CommandArgument='<%# Eval("relPath") %>'></asp:LinkButton>
                                            <asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("relPath") %>' runat="server" OnClick="DownloadFile"></asp:LinkButton>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:TemplateField>
                                </Columns>
                            <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" PageButtonCount="4" />
                            <PagerStyle CssClass="pgr" />
                        </asp:GridView>
                    </ContentTemplate>
                </asp:UpdatePanel>
                </div>

           
                <asp:UpdatePanel ID="UpdatePanel3" runat="server">
                    <ContentTemplate>
                        <asp:Literal ID="ltEmbed" runat="server" />
                    </ContentTemplate>
                </asp:UpdatePanel>
           

Open in new window


code-behind...
protected void gvWPAs_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvWPAs.PageIndex = e.NewPageIndex;
        var ctyID = CountyList.SelectedValue;
        var twnID = TwnList.SelectedValue;
        var rngID = RngList.SelectedValue;

        if (ctyID != "0" & twnID == "0" & rngID == "0") // County only
        {
            dsGetCtyWPAs.SelectParameters["ctyID"].DefaultValue = ctyID;
            DataView dvCtyWpa = (DataView)dsGetCtyWPAs.Select(DataSourceSelectArguments.Empty);
            //DataTable dtCtyWpa = dvCtyWpa.ToTable();
            gvWPAs.DataSource = dvCtyWpa;
            gvWPAs.DataBind();
            this.RegisterPostBackControl();
        }
        
    }


    protected void View(object sender, EventArgs e)
    {
        string pdfIdentifier = (sender as LinkButton).CommandArgument;
        string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"650px\" height=\"850px\">";
        embed += "If you are unable to view file, you can download from <a href = \"{0}{1}&download=1\">here</a>";
        embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
        embed += "</object>";
        ltEmbed.Text = string.Format(embed, ResolveUrl("~/PdfHandler.ashx?pdfPath="), "WpaFiles/" + pdfIdentifier);
        
    }



    protected void DownloadFile(object sender, EventArgs e)
    {
        string filePath = (sender as LinkButton).CommandArgument;        
        string fileName = Path.GetFileName(filePath);        
        Downloader.Download(fileName, "WpaFiles/" + filePath);
    }

Open in new window

0
Comment
Question by:Nate_LR
  • 3
4 Comments
 
LVL 8

Expert Comment

by:Naitik Gamit
ID: 40600553
use IsPostBack Property...
0
 

Author Comment

by:Nate_LR
ID: 40600788
Ok, like this?....
protected void gvWPAs_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (!Page.IsPostBack)
        {
            gvWPAs.PageIndex = e.NewPageIndex;
            var ctyID = CountyList.SelectedValue;
            var twnID = TwnList.SelectedValue;
            var rngID = RngList.SelectedValue;

            if (ctyID != "0" & twnID == "0" & rngID == "0") // County only
            {
                dsGetCtyWPAs.SelectParameters["ctyID"].DefaultValue = ctyID;
                DataView dvCtyWpa = (DataView)dsGetCtyWPAs.Select(DataSourceSelectArguments.Empty);
                gvWPAs.DataSource = dvCtyWpa;
                gvWPAs.DataBind();
                this.RegisterPostBackControl();

            }
            else if (ctyID != "0" & twnID != "0" & rngID != "0") //County, township, and range
            {
                dsGetCTRWPAs.SelectParameters["ctyID"].DefaultValue = ctyID;
                dsGetCTRWPAs.SelectParameters["twnID"].DefaultValue = twnID;
                dsGetCTRWPAs.SelectParameters["rngID"].DefaultValue = rngID;
                DataView dvCTRWpa = (DataView)dsGetCTRWPAs.Select(DataSourceSelectArguments.Empty);
                gvWPAs.DataSource = dvCTRWpa;
                gvWPAs.DataBind();
                this.RegisterPostBackControl();

            }
            else if (ctyID == "0" & twnID != "0" & rngID != "0") // Township and Range
            {
                dsGetTRWPAs.SelectParameters["twnID"].DefaultValue = twnID;
                dsGetTRWPAs.SelectParameters["rngID"].DefaultValue = rngID;
                DataView dvTRWpa = (DataView)dsGetTRWPAs.Select(DataSourceSelectArguments.Empty);
                gvWPAs.DataSource = dvTRWpa;
                gvWPAs.DataBind();
                this.RegisterPostBackControl();
            }
        }
    }


    protected void View(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string pdfIdentifier = (sender as LinkButton).CommandArgument;
            string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"650px\" height=\"850px\">";
            embed += "If you are unable to view file, you can download from <a href = \"{0}{1}&download=1\">here</a>";
            embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
            embed += "</object>";
            ltEmbed.Text = string.Format(embed, ResolveUrl("~/PdfHandler.ashx?pdfPath="), "WpaFiles/" + pdfIdentifier);
            this.RegisterPostBackControl();
        }
    }

Open in new window

They don't work if I do that, so is that how you mean?
0
 

Accepted Solution

by:
Nate_LR earned 0 total points
ID: 40604584
I set the UpdateMode to Conditional on UpdatePanel3
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">                     
                <ContentTemplate>
                    <asp:Literal ID="ltEmbed" runat="server" />
                </ContentTemplate>
            </asp:UpdatePanel>

Open in new window

Then added UpdatePanel3.Update() to....
protected void View(object sender, EventArgs e)
{
string pdfIdentifier = (sender as LinkButton).CommandArgument;
string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"650px\" height=\"850px\">";
embed += "If you are unable to view file, you can download from <a href = \"{0}{1}&download=1\">here</a>";
embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
embed += "</object>";
ltEmbed.Text = string.Format(embed, ResolveUrl("~/PdfHandler.ashx?pdfPath="), "WpaFiles/" + pdfIdentifier);
UpdatePanel3.Update();
}

Open in new window

0
 

Author Closing Comment

by:Nate_LR
ID: 40611868
I figured it out with no help from "Experts"
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question