/
PostAd.aspx.cs
executable file
·305 lines (270 loc) · 9.77 KB
/
PostAd.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System;
using System.Text;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using AspNet.StarterKits.Classifieds.Web;
using AspNet.StarterKits.Classifieds.BusinessLogicLayer;
public partial class PostAd_aspx : System.Web.UI.Page
{
public int PreviousAdId
{
get
{
if (ViewState["PreviousAdId"] != null)
return (int)ViewState["PreviousAdId"];
else
return 0;
}
set
{
ViewState["PreviousAdId"] = value;
}
}
public bool IsPreviousAd
{
get
{
return (ViewState["PreviousAdId"] != null);
}
}
private const string OtherLocationText = "Other...";
protected void PostAdWizard_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
if (Page.IsValid)
{
int memberId = Profile.MemberId;
int categoryId = CategoryPath.CurrentCategoryId;
string title = Server.HtmlEncode(TitleTextBox.Text);
string description = Server.HtmlEncode(DescriptionTextBox.Text);
string code = Server.HtmlEncode(TextBoxCode.Text);
string url; //= Server.UrlEncode(UrlTextBox.Text);
Decimal price = 0;//Decimal.Parse(PriceTextBox.Text);
string syn1 = Server.HtmlEncode(TextBoxSyn1.Text);
string syn2 = Server.HtmlEncode(TextBoxSyn2.Text);
string syn3 = Server.HtmlEncode(TextBoxSyn3.Text);
string syn4 = Server.HtmlEncode(TextBoxSyn4.Text);
string syn5 = Server.HtmlEncode(TextBoxSyn5.Text);
string location = "";//Server.HtmlEncode(LocationDropDown.CurrentLocation);
int numDays = 210000;//Convert.ToInt32(NumDaysList.SelectedValue);
AdType adType = AdType.ForSale;
//if (Enum.IsDefined(typeof(AdType), Convert.ToInt32(AdTypeSelection.SelectedValue)))
// adType = (AdType)Enum.Parse(typeof(AdType), AdTypeSelection.SelectedValue);
url = UploadForm(sender, e);
//if (!CheckFileValid())
//{
// e.Cancel = true;
//}
if (IsPreviousAd && url != null)
{
AdsDB.RelistAd(PreviousAdId,
categoryId,
title,
description,
url,
price,
location,
numDays,
AdLevel.Unspecified,
AdStatus.Unspecified,
adType,
code, syn1, syn2, syn3, syn4, syn5);
Response.Redirect("~/MyAds.aspx", true);
}
else if (url != null)
{
int adId = AdsDB.InsertAd(memberId,
categoryId,
title,
description,
url,
price,
location,
numDays,
AdLevel.Unspecified,
AdStatus.Unspecified,
adType,
code, syn1, syn2, syn3, syn4, syn5);
SiteSettings s = SiteSettings.GetSharedSettings();
//UploadImagesLink.Visible = s.AllowImageUploads;
//UploadImagesLink.NavigateUrl = "~/ManagePhotos.aspx?id=" + adId.ToString();
}
else
{
e.Cancel = true;
}
}
else
e.Cancel = true;
}
protected void Page_Load(object sender, EventArgs e)
{
// Limit Description text.
DescriptionTextBox.Attributes.Add("onkeydown", "textCounter(this,500);");
DescriptionTextBox.Attributes.Add("onkeyup", "textCounter(this,500);");
if (!Page.IsPostBack)
{
PostAdWizard.MoveTo(PostAdWizard.WizardSteps[0]);
string qsRelistId = Request.QueryString["relist"];
if (qsRelistId != null)
{
int adId;
if (Int32.TryParse(qsRelistId, out adId))
{
LoadPreviousAd(adId);
}
}
}
}
//adds Code, Name, Synonyms to database in addition to URL of uploaded file
protected string UploadForm(object sender, EventArgs e)
{
//if upload field is empty
if (IsPostBack)
{
if (FormUpload.HasFile == false)
{
UploadDetails.Text = "Please first select a file to upload...";
}
//otherwise proceed
else
{
// Save the file
string filePath =
Server.MapPath("~/Upload/Forms/"
+ FormUpload.FileName);
FormUpload.SaveAs(filePath);
/*END file UPLOAD file code*/
return "~/Upload/Forms/" + FormUpload.FileName;
//}
//else
//{
// return null;
//}
}
}
return "no dice!";
}
protected bool CheckFileValid()
{
//[CHECK if empty]//
if (FormUpload.FileName == "" || FormUpload.FileName == null)
{
return false;
}
else
{
//[check file extensions]//
String fileExtension =
System.IO.Path.GetExtension(FormUpload.FileName).ToLower();
String[] allowedExtensions =
{ ".pdf", ".doc", ".DOC",
".rtf", ".RTF", ".PDF",
".xls", ".XLS"}; //allowing the usuall suspects for now
//parse the extension for a match, no match it will fall through at next conditional stmt.
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{ return true; }
}
}
return false;
}
protected void LoadPreviousAd(int adId)
{
AdsDataComponent.AdsRow ad = AdsDB.GetAdById(adId);
if (ad != null)
{
if (ad.MemberId == Profile.MemberId)
{
PreviousAdId = adId;
SetCurrentCategory(ad.CategoryId);
//if ((AdType)ad.AdType == AdType.Wanted)
// AdTypeSelection.SelectedIndex = 1;
//else
// AdTypeSelection.SelectedIndex = 0;
TitleTextBox.Text = ad.Title;
DescriptionTextBox.Text = ad.Description;
//UrlTextBox.Text = ad.URL;
//LocationDropDown.CurrentLocation = ad.Location;
}
}
}
protected void UpdateCategoryDisplay()
{
this.CategoryPathLabel.Text = CategoryPath.FullCategoryPath;
}
protected void SubcategoriesDS_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
List<CachedCategory> subCategories = e.ReturnValue as List<CachedCategory>;
if (subCategories == null || subCategories.Count == 0)
{
PostAdWizard.MoveTo(PostAdWizard.WizardSteps[1]);
}
}
protected void SetCurrentCategory(int categoryId)
{
CategoryPath.CurrentCategoryId = categoryId;
UpdateCategoryDisplay();
}
protected void SubcategoriesList_ItemCommand(object source, DataListCommandEventArgs e)
{
int categoryId = Convert.ToInt32(e.CommandArgument);
SetCurrentCategory(categoryId);
}
protected void CategoryPath_CategorySelectionChanged(object sender, CategorySelectionChangedEventArgs e)
{
UpdateCategoryDisplay();
}
protected void ChangeCategoryButton_Click(object sender, EventArgs e)
{
SetCurrentCategory(DefaultValues.CategoryIdMinValue);
PostAdWizard.MoveTo(PostAdWizard.WizardSteps[0]);
}
protected void PostAdWizard_PreviousButtonClick(object sender, WizardNavigationEventArgs e)
{
if (e.NextStepIndex == 0)
{
SetCurrentCategory(DefaultValues.CategoryIdMinValue);
}
}
/*protected void ValidLocationRequired_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = !LocationDropDown.CurrentLocation.Equals(String.Empty);
}*/
/*protected void PriceValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
decimal p = -1;
if (decimal.TryParse(PriceTextBox.Text, out p))
{
args.IsValid = (p >= 0);
}
else
args.IsValid = false;
}*/
protected void URLValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
TextBox URLTextBox = AdDetailsStep.FindControl("UrlTextBox") as TextBox;
if (URLTextBox != null && !URLTextBox.Text.Equals(String.Empty))
{
try
{
Uri uri = new Uri(URLTextBox.Text);
if (uri.IsWellFormedOriginalString() &
(uri.Scheme == "http" | uri.Scheme == "https"))
{
args.IsValid = true;
}
}
catch
{
}
}
else
{
// Empty URL is okay.
args.IsValid = true;
}
}
}