Mohammad Imran

Mohammad Imran

  • 1.5k
  • 172
  • 13.4k

How to open pdf file new tab in browser in ASP.NET C#

Aug 28 2020 11:59 PM
When select an item from dropdownlist it will open a file in new tab. There is no error. but when I click on link button,click event does not fire of link button.
 
Again dropdownlist SelectedIndexChanged event is fired when I click on link button.
 
When I select from dropdownlist , it open a file new tab but dows call javascript function "ResetTarget()"
  1. <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">  
  3. <script type="text/javascript">  
  4. function ResetTarget()  
  5. {  
  6. alert('Yes');  
  7. document.forms[0].target = '';  
  8. }  
  9. function SetTarget()  
  10. {  
  11. document.forms[0].target = "_blank";  
  12. window.setTimeout("document.forms[0].target='';", 500);  
  13. }  
  14. </script>  
  15. <asp:HiddenField ID="hidCSRF" runat="server" Value="" />  
  16. <table align="center" class="SubFormWOBG" style="line-height: 25px; width: 100%">  
  17. <tr>  
  18. <td style="width: 40%">Application Code: </td>  
  19. <td style="width: 60%">  
  20. <asp:LinkButton ID="lnkApplicationCode" runat="server" CausesValidation="false" OnClick="lnkApplicationCode_click"></asp:LinkButton>  
  21. </td> </tr>  
  22. <tr runat="server" id="MandatAtt">  
  23. <td>Mandatory Attchment:</td>  
  24. <td>  
  25. <asp:DropDownList runat="server" ID="ddlAttachment" AutoPostBack="true" OnSelectedIndexChanged="ddlAttachment_SelectedIndexChanged"> </asp:DropDownList>  
  26. </td>  
  27. </tr>  
  28. </table>  
  29. </asp:Content>  
in c# file
  1. protected void Page_Load(object sender, EventArgs e) {  
  2.   if (!IsPostBack) {  
  3.     try {  
  4.       lnkApplicationCode.Text = "135";  
  5.       MandatAtt.Visible = false;  
  6.       ddlAttachment.Attributes["onchange"] = "SetTarget();";  
  7.       MandatAtt.Visible = true;  
  8.     } catch(Exception) {  
  9.       Response.Redirect("~/ErrorPage.aspx"false);  
  10.     } finally {}  
  11.   }  
  12. }  
  13. protected void ddlAttachment_SelectedIndexChanged(object sender, EventArgs e) {  
  14.   try {  
  15.     string[] arr = ddlAttachment.SelectedValue.ToString().Split(',');  
  16.     int int_attachmentCode = Convert.ToInt32(arr[0]);  
  17.     int int_attachmentSerialCode = Convert.ToInt32(arr[1]);  
  18.     NOCAPInternalUtility.INDAppViewFiles(Convert.ToInt64(lnkApplicationCode.Text.Trim()), int_attachmentCode, int_attachmentSerialCode);  
  19.   } catch(System.Threading.ThreadAbortException ex) {} catch(Exception) {  
  20.     Response.Redirect("~/ErrorPage.aspx"false);  
  21.   } finally {  
  22.     hidCSRF.Value = Convert.ToString(System.Guid.NewGuid());  
  23.     ViewState["CSRF"] = hidCSRF.Value;  
  24.     ScriptManager.RegisterStartupScript(this, GetType(), "function""ResetTarget();"false);  
  25.   }  
  26. }  
  27. protected void lnkApplicationCode_click(object sender, EventArgs e) {  
  28.   hidCSRF.Value = Convert.ToString(System.Guid.NewGuid());  
  29.   ViewState["CSRF"] = hidCSRF.Value;  
  30.   try {  
  31.     long ApplicationCode = Convert.ToInt64(lnkApplicationCode.Text);  
  32.     Session["ApplicationCode"] = ApplicationCode;  
  33.   } catch(Exception) {  
  34.     Response.Redirect("~/ErrorPage.aspx"false);  
  35.   }  
  36. }  

Attachment: SC.rar

Answers (2)