-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditAd.aspx.cs
executable file
·234 lines (213 loc) · 8.01 KB
/
EditAd.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
using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.IO;
using AspNet.StarterKits.Classifieds.BusinessLogicLayer;
public partial class EditAd_aspx : System.Web.UI.Page
{
public int NumPostBacks
{
get
{
if (ViewState["NumPostBacks"] != null)
return (int)ViewState["NumPostBacks"];
else
{
ViewState["NumPostBacks"] = 0;
return 0;
}
}
set
{
ViewState["NumPostBacks"] = value;
}
}
private string UrlReferrer
{
get
{
return ViewState["UrlReferrer"] as string;
}
set
{
ViewState["UrlReferrer"] = value;
}
}
protected void CancelButton_Click(object sender, EventArgs e)
{
ReturnToPreviousPage();
}
protected void AdDataSource_Updated(object sender, ObjectDataSourceStatusEventArgs e)
{
ReturnToPreviousPage();
}
protected void ChangeCategoryButton_Click(object sender, EventArgs e)
{
ChangeCategoryPanel.Visible = true;
AdFormView.Enabled = false;
}
protected void ChangeCategoryCancelButton_Click(object sender, EventArgs e)
{
ChangeCategoryPanel.Visible = false;
AdFormView.Enabled = true;
}
protected void ChangeCategoryOkButton_Click(object sender, EventArgs e)
{
if (CategoryDropDown.SelectedCategoryId != DefaultValues.CategoryIdMinValue)
{
int adId = Convert.ToInt32(this.AdFormView.DataKey.Value);
AdsDB.UpdateAdCategory(adId, CategoryDropDown.SelectedCategoryId);
AdFormView.DataBind();
}
ChangeCategoryPanel.Visible = false;
AdFormView.Enabled = true;
}
protected void AdDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
bool adValid = false;
AdsDataComponent.AdsRow ad = e.ReturnValue as AdsDataComponent.AdsRow;
if (ad != null)
{
if (User.IsInRole(DefaultValues.AdministratorRole) || ad.MemberId == Profile.MemberId)
{
adValid = true;
string adIdString = ad.Id.ToString();
CategoryDropDown.CurrentCategoryId = ad.CategoryId;
ManagePhotosLink.NavigateUrl = "~/ManagePhotos.aspx?id=" + adIdString;
ShowAdLink.NavigateUrl = "~/ShowAd.aspx?id=" + adIdString;
}
}
if (!adValid)
{
ReturnToPreviousPage();
}
}
protected void ReturnToPreviousPage()
{
string referrer = UrlReferrer;
if (referrer != null)
Response.Redirect(referrer);
else
Response.Redirect("~/MyAds.aspx", true);
}
protected void Page_Load(object sender, EventArgs e)
{
// Check if the URL querystring contains a valid ad.
int adId = DefaultValues.IdNullValue;
string adIdQs = Request.QueryString["id"];
if (adIdQs == null || !Int32.TryParse(adIdQs, out adId))
{
Response.Redirect("~/MyAds.aspx");
}
// Limit Description text.
TextBox DescriptionTextBox = AdFormView.FindControl("DescriptionTextBox") as TextBox;
DescriptionTextBox.Attributes.Add("onkeydown", "textCounter(this,500);");
DescriptionTextBox.Attributes.Add("onkeyup", "textCounter(this,500);");
if (!Page.IsPostBack)
{
ManagePhotosLinkPanel.Visible = SiteSettings.GetSharedSettings().AllowImageUploads;
if (Request.UrlReferrer != null)
this.UrlReferrer = Request.UrlReferrer.ToString();
}
else
NumPostBacks++;
int goBackSteps = NumPostBacks + 1;
BackLink.NavigateUrl = String.Format("javascript:history.go(-{0});", goBackSteps);
}
protected void ValidLocationRequired_ServerValidate(object source, ServerValidateEventArgs args)
{
LocationDropDown_ascx LocationDropDown = AdFormView.FindControl("LocationDropDown") as LocationDropDown_ascx;
args.IsValid = LocationDropDown != null && !LocationDropDown.CurrentLocation.Equals(String.Empty);
}
protected void PriceValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
decimal p = -1;
TextBox PriceTextBox = AdFormView.FindControl("PriceTextBox") as TextBox;
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 = AdFormView.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;
}
}
protected void AdFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
if (!Page.IsValid)
e.Cancel = true;
}
//occurs when upload button is clicked
protected void ChangeFile(object sender, EventArgs e)
{
string oldfilepath;
string newfilepath;
string emailmessage;
TextBox URLTextBox = AdFormView.FindControl("URLTextBox") as TextBox;
TextBox NewFilePath = AdFormView.FindControl("NewFilePath") as TextBox;
FileUpload FileUpload = AdFormView.FindControl("FileUploadToChangeURLTextBox") as FileUpload;
Label UploadMsg = AdFormView.FindControl("UploadMsg") as Label;
oldfilepath = URLTextBox.Text;
emailmessage = "Form " + ((TextBox)AdFormView.FindControl("TitleTextBox")).Text + " (" +
((TextBox)AdFormView.FindControl("TextBoxCode")).Text + " has had it's file replaced";
//if user has opted to Upload new file
if (IsPostBack)
{
if (FileUpload.HasFile)
{
try
{
//has to fill in the root with the server path
newfilepath = Server.MapPath("~/Upload/Forms/" +
FileUpload.FileName);
FileUpload.SaveAs(newfilepath);
UploadMsg.Text = "File name: " +
FileUpload.PostedFile.FileName + "<br>" +
FileUpload.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUpload.PostedFile.ContentType;
//NEW!!! send webmaster email.
//email says file is uploaded...
//may or may not be associated with database (send title as well)
emailmessage += "/nFile: [" + oldfilepath +
"] has been replaced with [" + newfilepath + "].";
//update file path in textbox to update DB when user clicks apply changes
NewFilePath.Text = newfilepath; //vestigal, used in testting only, dont delete yet
URLTextBox.Text = newfilepath;
}
catch (Exception ex)
{
UploadMsg.Text = "ERROR: " + ex.Message.ToString();
}
}
else
{
UploadMsg.Text += "No File Chosen.";
}
}
}
}