Solved

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

Posted on 2015-02-09
4
159 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this first video of the three-part Xpdf series, we introduce and describe Xpdf, a library containing nine command line utilities that perform various functions on PDF files. We show where the library is located and how to download it, discuss its…
Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…

828 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