<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
按鈕打開,而不是在當前窗口上運行搜索。我必須刷新頁面才能再次搜索當前窗口,即使新窗口已經打開。
如何解決我遇到的問題?
我改變了默認事件爲false,所以它是一個按鈕,而不是「提交」,但它是做同樣的事情。我猜這是因爲OnClientClick方法。 – SearchForKnowledge
我不能在超鏈接中使用'Session [「fileName」]',因爲它不在代碼後面。我如何訪問? – SearchForKnowledge