Solved

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

Posted on 2015-02-09
4
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

*Adobe Acrobat 9 was used for this article. Particular steps may vary depending on software versions. 1. Create a framework of your form in Word, leaving space where you’d ultimately like the Adobe fields to appear.  (Note: I use the blank lines …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
We often encounter PDF files that are pure images, that is, they do not have text characters, but instead contain only raster graphics. The most common causes of this are document scanning software and faxing software/services that create image-only…
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

739 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