2014-07-14 44 views
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> 

在中繼器中搜索並顯示結果後,我允許用戶通過單擊鏈接按鈕打開一個新窗口來查看瀏覽器中的文件:爲什麼使用IHttpHandler導致我的提交按鈕打開新窗口

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

後面的代碼是這樣的:

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

OpenFilePDF.ashx代碼:

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; 
    } 
} 

正在發生的情景是,在搜索並顯示結果後,如果用戶單擊了View in Browser按鈕並返回到舊窗口並單擊了Validate Name按鈕,結果會出現在最初的窗口中通過點擊View in Browser按鈕打開,而不是在當前窗口上運行搜索。我必須刷新頁面才能再次搜索當前窗口,即使新窗口已經打開。

如何解決我遇到的問題?

回答

2

View in Browser實現爲

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

在客戶端點擊,你的網頁的表單元素看起來像

<form ... target="_blank">

造成任何提交表單打開的在新窗口中。但是,每次向ASP.NET回發都會提交表單,因此所有內容都將在新窗口中打開。您可以考慮在鏈接上設置屬性,而不是在表單上設置target屬性。要做到這一點,看到這樣的回答:

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

請記住,你將不得不使用一個HyperLink代替LinkButton的。實際上,這會讓您的回程減少1次,因爲您使用LinkButton重定向到其他網址。因此,您的HyperLink只能有一個href指向"OpenFilePDF.ashx?fileVar=" + Session["fileName"]開始。

編輯/ TL; DR:

變化

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

代碼隱藏:

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

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

標記:

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

+0

我改變了默認事件爲false,所以它是一個按鈕,而不是「提交」,但它是做同樣的事情。我猜這是因爲OnClientClick方法。 – SearchForKnowledge

+0

我不能在超鏈接中使用'Session [「fileName」]',因爲它不在代碼後面。我如何訪問? – SearchForKnowledge

相關問題