0
<asp:Panel ID="pnlSearch" runat="server" DefaultButton="btnValidateName">
    <div class="spPad">
        <b class="userLabel">Enter Full/Partial First or Last Name to Search (Leave blank for all):</b>
    </div>
    <div class="spPad" style="text-align: right;">
        <asp:TextBox ID="txtName" Width="95%" runat="server" ClientIDMode="Static"></asp:TextBox>
    </div>
    <div class="spPad" style="text-align: right;">
        <asp:Button ID="btnValidateName" CssClass="orange button" runat="server" Text="Validate Name" onclick="btnValidateName_Click" />
    </div>
    <div class="spPad" style="text-align: right;">
        <asp:Label runat="server" Text="" ID="lblIsValid"></asp:Label>
    </div>
</asp:Panel>

After searching and displaying the result inside a repeater, I allow user to view the file in the browser by opening a new window by clicking on a linkbutton:

<asp:LinkButton ID="lnkView" Text="View in Browser" OnClientClick="window.document.forms[0].target='blank';" runat="server" OnClick="ViewFile" />

The code behind is this:

protected void ViewFile(object sender, EventArgs e)
{
    Response.Redirect("OpenFilePDF.ashx?fileVar=" + Session["fileName"]);
}

The OpenFilePDF.ashx code is:

public void ProcessRequest (HttpContext context) {
    System.Web.HttpRequest request2 = System.Web.HttpContext.Current.Request;
    string strSessVar2 = request2.QueryString["fileVar"];

    try
    {
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.ClearContent();
        response.Clear();
        response.ContentType = "application/pdf";
        byte[] fileByteArray = File.ReadAllBytes(Path.Combine(@"C:\PDF", strSessVar2));
        response.AddHeader("Content-disposition", String.Format("inline; filename={0}", strSessVar2));
        response.BinaryWrite(fileByteArray);
        response.End();
    }
    catch (Exception ce)
    {

    }
}

public bool IsReusable {
    get {
        return false;
    }
}

The scenario that is happening is, after the search and displaying the result, if the user clicks on the View in Browser button and comes back to the old window and clicks on the Validate Name button the result goes in the window that was originally opened by clicking on View in Browser button instead of running the search on the current window. I have to refresh the page for it to search on the current window again, even if the new window was already opened.

How do I resolve the issue I am having?

SearchForKnowledge
  • 3,663
  • 9
  • 49
  • 122

1 Answers1

2

Your View in Browser is implemented as

<asp:LinkButton ID="lnkView" Text="View in Browser" OnClientClick="window.document.forms[0].target='blank';" runat="server" OnClick="ViewFile" />

On the client click, the form element of your web page will look like

<form ... target="_blank">

Causing any submit of the form to open in a new window. However, every single postback to ASP.NET constitutes a submission of your form, so everything will open in a new window. Rather than setting the target attribute on the form, you might consider setting the attribute just on the link. To do that, see this answer:

https://stackoverflow.com/a/2637208/1981387

Keep in mind, you will have to use a HyperLink instead of a LinkButton. Actually, this will benefit you by reducing the round trip back to your server by 1, because you are using the LinkButton to redirect to another url anyways. So your HyperLink can just have an href that points to "OpenFilePDF.ashx?fileVar=" + Session["fileName"] to begin with.

Edit/TL;DR:

Change

<asp:LinkButton ID="lnkView" Text="View in Browser" OnClientClick="window.document.forms[0].target='blank';" runat="server" OnClick="ViewFile" />

To

code-behind:

public string FileLocation
{   
   get
   {
      return "OpenFilePDF.ashx?fileVar=" + Session["fileName"];
   }
}

protected void Page_Load(object sender, EventArgs e)
{
...
lnkViewProper.NavigateUrl = FileLocation;
...
}

markup:

<asp:HyperLink ID="lnkViewProper" Text="View in Browser" runat="server" Target="_blank" />

Community
  • 1
  • 1
welegan
  • 3,013
  • 3
  • 15
  • 20