Reputation: 27
I have some problem with my coding to open my pdf file at new tab or new window. Then, I'm also have problem in filtering my gridview. This is because when the gridview was filtered the download button can not work and the pdf can't be download. Here is my aspx coding
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewDocument.aspx.cs" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<center>
<br />
<br />
<asp:Label ID="Label1" runat="server" CssClass="title" Text="DOCUMENT LIST"></asp:Label>
<br />
<br />
<br />
<table>
<tr>
<td style="text-align: center; padding-left: 25px; padding-right: 25px">
</td>
<td style="text-align: center">
<asp:Button ID="Button1" runat="server" CssClass="button" Text="Contract(Test)" OnClick="Button1_Click" />
</td>
<td style="text-align: center; padding-left: 25px; padding-right: 25px">
</td>
</tr>
</table>
<br />
<br />
</center>
<asp:MultiView ID="MultiView1" runat="server">
<asp:View ID="DocView" runat="server">
<center>
<br />
<br />
<table>
<tr>
<td style="text-align: center; padding-left: 10px; padding-right: 15px">
<asp:Label ID="Label2" runat="server" Text="Search By :"></asp:Label>
</td>
<%-- <td style="text-align: center; padding-left: 5px; padding-right: 5px">
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="dropDownMenu">
<asp:ListItem Value="Document Name"></asp:ListItem>
<asp:ListItem Value="Date"></asp:ListItem>
</asp:DropDownList>
</td>--%>
<td style="padding-left: 5px; padding-right: 5px">
<asp:TextBox ID="DocSearch" runat="server" CssClass="text-input" Width="200px" MaxLength="100"
Font-Names="verdana"></asp:TextBox>
</td>
<td>
<asp:Button ID="Button2" runat="server" CssClass="button" Text="Search" OnCommand="Button2_Command" />
</td>
</tr>
</table>
<br />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderStyle="Solid"
BorderColor="#ccc" BorderWidth="2px" GridLines="Horizontal" Width="650px" AutoGenerateColumns="false"
CellPadding="3" OnRowCommand="ActionCommand" AllowSorting="true">
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<Columns>
<asp:BoundField DataField="folderName">
<ItemStyle CssClass="leftAligment" />
</asp:BoundField>
<asp:BoundField DataField="fileName" HeaderText="DOCUMENT NAME">
<ItemStyle CssClass="leftAligment" />
</asp:BoundField>
<asp:BoundField DataField="Date" HeaderText="DATE / TIME">
<ItemStyle CssClass="centerAlign" />
</asp:BoundField>
<asp:ButtonField CommandName="Open" Text="Open" ButtonType="Link" />
<asp:ButtonField CommandName="Download" Text="Download" />
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>
</center>
<br />
<br />
<br />
</asp:View>
</asp:MultiView>
my aspx.cs coding
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using System.Threading;
using System.Net;
using System.Xml;
namespace Hibah_Total_v1._2.Secure
{
public partial class ViewDocument : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MultiView1.Visible = false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
MultiView1.Visible = true;
MultiView1.SetActiveView(DocView);
string folderName = ConfigurationManager.AppSettings["folderPDF"].ToString();
string path = Server.MapPath("~") + "/Secure/";
string fullPath = path + folderName;
string[] filePaths = Directory.GetFiles(fullPath, "*.pdf");
DataTable table = GetTable(filePaths);
GridView1.Columns[0].Visible = true;
GridView1.DataSource = table;
GridView1.DataBind();
GridView1.Columns[0].Visible = false;
}
public DataTable GetTable(string[] filePaths)
{
DataTable table = new DataTable();
table.Columns.Add("folderName", typeof(string));
table.Columns.Add("fileName", typeof(string));
table.Columns.Add("Date", typeof(string));
DataRow row;
{
for (int i = 0; i < filePaths.Length; i++)
{
row = table.NewRow();
row["folderName"] = filePaths[i];
FileInfo Myfile = new FileInfo(filePaths[i]);
row["fileName"] = Myfile.Name.ToString();
row["Date"] = Myfile.CreationTime.ToString();
table.Rows.Add(row);
}
}
Session["Table"] = table;
this.GridView1.DataSource = ((DataTable)Session["Table"]).DefaultView;
this.GridView1.DataBind();
return table;
}
public void ActionCommand(object sender, GridViewCommandEventArgs e)
{
string commandName = e.CommandName.ToString().Trim();
GridViewRow row = GridView1.Rows[Convert.ToInt32(e.CommandArgument)];
string folderName = ConfigurationManager.AppSettings["folderPDF"].ToString();
string path = Server.MapPath("~") + "/Secure/";
string fullPath = path + folderName;
string[] filePaths = Directory.GetFiles(fullPath, "*.pdf");
switch (commandName)
{
case "Open":
ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('OpenForm.aspx?path=" + row.Cells[0].Text + "');", true);
break;
case "Download":
FileStream fs = new FileStream(row.Cells[0].Text, FileMode.Open, FileAccess.Read);
byte[] ar = new byte[(int)fs.Length];
fs.Read(ar, 0, (int)fs.Length);
fs.Close();
Response.AddHeader("content-disposition", "attachment;fileName=" + HttpUtility.UrlEncode(row.Cells[1].Text, System.Text.Encoding.UTF8));
Response.ContentType = "application/octectstream";
Response.BinaryWrite(ar);
Response.End();
break;
default: break;
}
}
protected void Button2_Command(object sender, EventArgs e)
{
string folderName = ConfigurationManager.AppSettings["folderPDF"].ToString();
string path = Server.MapPath("~") + "/Secure/";
string fullPath = path + folderName;
string[] filePaths = Directory.GetFiles(fullPath, "*.pdf");
if (DocSearch.Text.ToString().Trim() != "")
{
DataTable dt = (DataTable)Session["Table"];
var query = from t in dt.AsEnumerable() where t.Field<string>("folderName").StartsWith(DocSearch.Text.ToString().Trim()) || t.Field<string>("folderName").Contains(DocSearch.Text.ToString().Trim()) select t;
DataTable dtable = new DataTable();
dtable = query.CopyToDataTable();
this.GridView1.DataSource = dtable;
this.GridView1.DataBind();
}
else
{
this.GridView1.DataSource = ((DataTable)Session["table"]).DefaultView;
this.GridView1.DataBind();
}
}
}
}
can anyone help me figure it out???
Upvotes: 1
Views: 799
Reputation: 43
private static void CreateCommand(string queryString,
string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
}
Upvotes: 1