Sunday, May 27, 2012

Open PDF file in new browser tab using ASP.NET with C#

Introduction
This tip describes how to open a PDF file in a new browser tab using ASP.NET with C#.

Using the code
We create a folder in our project and store the PDF in this folder.

  //Java Script for that
<script type="text/javascript">
    function openPDF() {
        var strMessage = '<%= p%>';
        var a = 'http://localhost:2878/your project name/' + strMessage;
        window.open(a, 'PDF');
        return true;
    }
</script>

// Create one public variable p.
public string p;
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
p = e.CommandArgument.ToString();
this.Page.ClientScript.RegisterStartupScript(this.GetType(),
"page_index_script","openPDF();",true);
}
//p variable is provide path..
//For Example:-"E-book/tejasvbhalani.pdf"
//E-book is our folder name and tejasvbhalani is our pdf file name..

11 comments:

Post a Comment

Followers