-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkSpaceCRUDController.aspx.cs
204 lines (177 loc) · 8.08 KB
/
LinkSpaceCRUDController.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Webkred;
using Newtonsoft.Json;
using System.Data;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Data.SqlClient;
namespace WebKred
{
public partial class LinkSpaceCRUDController : System.Web.UI.Page
{
LinkSpaceBAL linkSpaceBAL = new LinkSpaceBAL();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] == null && Session["UserName"].ToString() == "")
{
Response.Redirect("~/Login.aspx");
}
if (Session["ProjectId"] != null && Session["ProjectId"].ToString() != "")
{
linkSpaceBAL.ProjectId = Convert.ToInt32(Session["ProjectId"]);
}
Response.AddHeader("Content-type", "text/javascript");
if (Request.Params["p"].Equals("save"))
{
string lstr = Request.Params["_gt_json"];
SaveDataInfo<LinkSpaceEntityWrapper> ldi = JavaScriptConvert.DeserializeObject<SaveDataInfo<LinkSpaceEntityWrapper>>(lstr);
try
{
if (ldi.deletedRecords.Count() > 0)
{
linkSpaceBAL.DeleteLinkSpaces(ldi.deletedRecords);
}
else if (ldi.updatedRecords.Count() > 0)
{
linkSpaceBAL.UpdateLinkSpaces(ldi.updatedRecords);
}
else if (ldi.insertedRecords.Count() > 0)
{
linkSpaceBAL.InsertLinkSpaces(ldi.insertedRecords);
}
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
Response.Write("{success : true,exception:''}");
}
else if (Request.Params["p"].Equals("load"))
{
LinkSpaceEntity[] arrReturn = null;
int count = 0;
string lstr = Request.Params["_gt_json"];
LoadDataInfo ldi = JavaScriptConvert.DeserializeObject<LoadDataInfo>(lstr);
try
{
arrReturn = linkSpaceBAL.getLinkSpaceArray(ldi.pageInfo.PageNum, ldi.pageInfo.PageSize, linkSpaceBAL.ProjectId);
count = linkSpaceBAL.getLinkSpaceCount();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
string data = JavaScriptConvert.SerializeObject(arrReturn);
string ret = "{data:" + data + ",\n";
ret += "pageInfo:{totalRowNum:" + count + "},\n";
ret += "recordType : 'object'}";
Response.Write(ret);
}
else if (Request.Params["p"].Equals("xls") || Request.Params["p"].Equals("csv"))
{
string tab;
string csvFormate = string.Empty;
string attachment;
string columnName;
string rowData;
string lstr = Request.Params["_gt_json"];
SaveDataInfo<LinkSpaceEntityWrapper> ldi = JavaScriptConvert.DeserializeObject<SaveDataInfo<LinkSpaceEntityWrapper>>(lstr);
if (ldi.selectedRecords.Count() > 0)
{
if (Request.Params["p"].Equals("xls"))
{
attachment = "attachment; filename=LinkSpace.xls";
tab = "\t";
}
else
{
attachment = "attachment; filename=LinkSpace.csv";
tab = "\",\"";
csvFormate = "\"";
}
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
columnName = csvFormate + "Forum URL" + tab + "Forum Content" + tab + "Actor Name" + tab + "User Name" + tab + "Password" + tab + "Reply Content" + tab + "Reply URL" + csvFormate;
Response.Write(columnName);
Response.Write("\n");
foreach (LinkSpaceEntityWrapper lew in ldi.selectedRecords)
{
rowData = csvFormate + lew.ForumURL + tab + lew.ForumContent + tab + lew.ActorName.Substring(0, lew.ActorName.LastIndexOf(',')) + tab + lew.UserName + tab + lew.Password + tab + lew.ReplyContent + tab + lew.ReplyURL + csvFormate;
rowData = rowData.Replace("\n", " ").Replace("\n\r", " ");
Response.Write(rowData + "\n");
}
}
}
else if (Request.Params["p"].Equals("pdf"))
{
string lstr = Request.Params["_gt_json"];
SaveDataInfo<LinkSpaceEntityWrapper> ldi = JavaScriptConvert.DeserializeObject<SaveDataInfo<LinkSpaceEntityWrapper>>(lstr);
if (ldi.selectedRecords.Count() > 0)
{
DataTable dt = new DataTable("LinkSpace");
dt.Columns.Add("ForumURL", Type.GetType("System.String"));
dt.Columns.Add("ForumContent", Type.GetType("System.String"));
dt.Columns.Add("ActorName", Type.GetType("System.String"));
dt.Columns.Add("UserName", Type.GetType("System.String"));
dt.Columns.Add("Password", Type.GetType("System.String"));
dt.Columns.Add("ReplyContent", Type.GetType("System.String"));
dt.Columns.Add("ReplyURL", Type.GetType("System.String"));
DataRow dr;
foreach (LinkSpaceEntityWrapper lew in ldi.selectedRecords)
{
dr = dt.NewRow();
dr["ForumURL"] = lew.ForumURL;
dr["ForumContent"] = lew.ForumContent;
dr["ActorName"] = lew.ActorName.Substring(0, lew.ActorName.LastIndexOf(','));
dr["UserName"] = lew.UserName;
dr["Password"] = lew.Password;
dr["ReplyContent"] = lew.ReplyContent;
dr["ReplyURL"] = lew.ReplyURL;
dt.Rows.Add(dr);
}
ExportToPDF(dt);
}
}
else
{
Response.Write("Not Define");
}
Response.End();
}
protected void ExportToPDF(DataTable dt)
{
GridView gvExport = new GridView();
gvExport.AllowPaging = false;
gvExport.DataSource = dt;
gvExport.DataBind();
gvExport.HeaderRow.Style.Add("font-size", "10px");
gvExport.HeaderRow.Style.Add("font-weight", "bold");
gvExport.Style.Add("text-decoration", "none");
gvExport.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvExport.Style.Add("font-size", "8px");
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=LinkSpace.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvExport.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
}
}