/
NewTopic.aspx.cs
62 lines (50 loc) · 1.73 KB
/
NewTopic.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class NewTopic : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
int forumId;
if (!int.TryParse(Request.QueryString["forumId"], out forumId))
throw new HttpException(400, "Bad Request");
Forum forum = GetForum(forumId);
this.Title = LiteralPaginaTitel.Text = "Post new topic in " + forum.Name;
HiddenFieldForumId.Value = forum.Id.ToString();
(Master as Layout).GenerateBreadCrumb(forum);
(Master.FindControl("BulletedListBreadCrumb") as BulletedList).Items.Add(new ListItem("Posting new topic", "#"));
}
protected Forum GetForum(int id)
{
AspLinqDataContext dc = new AspLinqDataContext();
return (from Forum in dc.Forums where Forum.Id == id select Forum).Single();
}
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
AspLinqDataContext dc = new AspLinqDataContext();
Forum forum = GetForum(int.Parse(HiddenFieldForumId.Value));
// insert topic
Topic newTopic = new Topic();
newTopic.Title = TextBoxTitle.Text;
newTopic.ForumId = forum.Id;
newTopic.IsLocked = CheckBoxLock.Checked;
newTopic.IsPinned = CheckBoxPin.Checked;
dc.Topics.InsertOnSubmit(newTopic);
dc.SubmitChanges();
// insert post
Post newPost = new Post();
newPost.TopicId = newTopic.Id;
newPost.MemberId = (Session["member"] as Member).Id;
newPost.Content = TextBoxContent.Text;
newPost.CreatedDate = DateTime.Now;
newPost.FromIP = Request.UserHostAddress;
dc.Posts.InsertOnSubmit(newPost);
dc.SubmitChanges();
Response.Redirect("~/ViewTopic.aspx?id=" + newTopic.Id);
}
}