C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to open word,excel file on a new web page


Posted Date: 25 Aug 2008    Resource Type: Articles    Category: Web Applications

Posted By: Sunil Kumar       Member Level: Silver
Rating:     Points: 10



Hi Friends....

This code will help you to open word file that saved in database and shown through a Data List in a new window.
We are taking two forms one is for showing data from database as a link and when we click on button it show open/save dialog box and open it in a new web page. Hope it will help..

// This java script code is used to show a blank window when we call this function fun1 and pass the new page name as a parameter.
// write this code on in head section of your source page.

<script type="text/javascript">
function fun1(myurl)
{
var detailsWindow;
detailsWindow=window.open(myurl,"dfd","width=600,height=1000, resizable, scrollbars");
detailsWindow.focus();
return false;
}
</script>

Now see how to call this function.. its reslly very easy.. watch this code…
We are taking datalist containing a link button and one button when we click on link button it pick its itemcommand value that is its file name coming from database and shown in datalist as a link. And after click on button so it will call the function fun1() that open a new window…

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" OnItemCommand="DataList1_ItemCommand"
DataKeyField="FileExt">
<ItemTemplate>
FileExt:
<asp:LinkButton ID="lnk" runat="server" Text='<%# Eval("FileExt") %> '></asp:LinkButton>
<asp:Button ID="Open" Text="Open" runat="server" OnClientClick="return fun1('result.aspx')" />
<br />
</ItemTemplate>
</asp:DataList>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testingConnectionString %>"
SelectCommand="SELECT [FileExt] FROM [CVUpload]"></asp:SqlDataSource>

//This will bind data with datalist1


now on code behind file write this code on datalist item command event..

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
string name = (string)DataList1.DataKeys[e.Item.ItemIndex]; // get file name on which we clicked.
Session["FileName"] = name; // Used to retain file name on which we clicked

}

This get clicked value in string name and retain it in a session named FileName.



Now on result(Blank Page) write this code


protected void Page_Load(object sender, EventArgs e)
{
string name = Session["FileName"].ToString(); // getting file name from session and put it in local variable name.

Response.ContentType = "application/msword"; // set the application type here we use msword mime type you can change the MIME to other application.

Response.AppendHeader("Content-Disposition", "attachment; filename='"+name+"'"); // set the file name

Response.TransmitFile(Server.MapPath("~/Data/’”+name+”’”)); // Map the path from virtual directory.

Response.End(); // end of response

}

Use this its really works..




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Word File  .  Open xls file  .  Open xls  .  Open word file  .  Open excel file on other page  .  Open doc file  .  Open doc  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Records from Datagrid to Excel sheet
Previous Resource: Javascript ToolTip
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use