Tuesday, May 8, 2012

Open doc,txt or pdf files in C#

This example shows you how to open any doc,txt or pdf file which is saved in any specified location.In this example I m using a GridView which contains a template field which holds FileName.

<asp:TemplateField HeaderText="Application Name" SortExpression="ApplicationName">
       <ItemTemplate>
                       <asp:LinkButton ID="lbOpenApplication" runat="server" Text='<%# Bind("ApplicationName") %>'  CommandArgument='<%# Bind("ApplicationName") %>' CommandName="ViewApplication"></asp:LinkButton>
       </ItemTemplate>
</asp:TemplateField>

In Gridview's RowCommand event we have to write code snippet given below.

protected void gvViewLeave_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ViewApplication")
        {
            string filename = e.CommandArgument.ToString();
            string path = Server.MapPath("~/PayRoll/Applications/");
            System.Diagnostics.Process.Start(path + filename);
        }
    }

2 comments:

  1. Hi Anukana,

    I'm DAMAN please give me the solution of open .doc file in asp.net grid view using sql server. But i have already solution for downloading please help me... as soon as possible.

    ReplyDelete