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..
|
No responses found. Be the first to respond and make money from revenue sharing program.
|