Solved

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

Posted on 2015-02-09
4
144 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
Comment Utility
use IsPostBack Property...
0
 

Author Comment

by:Nate_LR
Comment Utility
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
Comment Utility
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
Comment Utility
I figured it out with no help from "Experts"
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Have you ever come up with a need of emailing only few pages of PDF file to one of yourfriend or colleague, instead of whole Adobe file? If yes, then surely you have face problems in doing that! Read this section as I have suggested multiple solutio…
*Adobe Acrobat 9 was used for this article.  Particular steps may vary depending on software versions. Adobe Acrobat has many, many variables that my be utilized to customize your forms for clarity and ease of use. The Form Editing Tool will be y…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now