| public partial class Sitefinity_ControlTemplates_Forums_SingleThread : System.Web.UI.UserControl |
| { |
| public void Page_Load(object sender, EventArgs e) |
| { |
| // subscribe for click events of our buttons |
| this.subscribeBtn.Click += new EventHandler(subscribeBtn_Click); |
| this.UnSubscribeBtn.Click += new EventHandler(UnSubscribeBtn_Click); |
| } |
| |
| // Here we have to check whether the user is subscribed to the forum or not |
| // and hide or show - subscribe and unsubscribe links |
| protected override void OnPreRender(EventArgs e) |
| { |
| base.OnPreRender(e); |
| if (!String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["mode"]) && !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["thread"])) |
| { |
| Guid ThreadID = new Guid(HttpContext.Current.Request.QueryString["thread"].ToString()); |
| ForumSubscriptionDataContext dataContext = new ForumSubscriptionDataContext(); |
| //we are sure that this query it will only result in one record - the current user email. So we use |
| // SingleOrDefault(); Returns the only element of a sequence, or a default value if the sequence is empty; |
| //this method throws an exception if there is more than one element in the sequence. |
| var query = (from sub in dataContext.ForumSubscribers |
| where sub.ThreadID == ThreadID |
| select sub.UserEmail).SingleOrDefault(); |
| if (query != null) |
| { |
| subscribeBtn.Visible = false; |
| UnSubscribeBtn.Visible = true; |
| } |
| else |
| { |
| subscribeBtn.Visible = true; |
| UnSubscribeBtn.Visible = false; |
| |
| } |
| } |
| } |
| /// <summary> |
| /// remove an user from the database |
| /// </summary> |
| /// <param name="sender"></param> |
| /// <param name="e"></param> |
| void UnSubscribeBtn_Click(object sender, EventArgs e) |
| { |
| Guid ThreadID = new Guid(HttpContext.Current.Request.QueryString["thread"].ToString()); |
| string uName = Telerik.Security.UserManager.GetCurrentUserName(); |
| MembershipUser user = Membership.GetUser(uName); |
| Guid userID = new Guid(user.ProviderUserKey.ToString()); |
| string eMail = user.Email; |
| ForumSubscriptionDataContext dataContext = new ForumSubscriptionDataContext(); |
| var query = from sub in dataContext.ForumSubscribers |
| where sub.ThreadID == ThreadID && sub.UserID == userID |
| select sub; |
| var record = query.First<ForumSubscriber>(); |
| dataContext.ForumSubscribers.DeleteOnSubmit(record); |
| dataContext.SubmitChanges(); |
| subscriptionMessage.Text = "<span style=background-color:Red;>YOU HAVE BEEN SUCCESSFULLY UNSUBSCRIBED</span>"; |
| } |
| /// <summary> |
| /// add user to the datbase |
| /// </summary> |
| /// <param name="sender"></param> |
| /// <param name="e"></param> |
| void subscribeBtn_Click(object sender, EventArgs e) |
| { |
| if (!String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["mode"]) && !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["thread"])) |
| { |
| // get the thread ID. |
| Guid ThreadID = new Guid(HttpContext.Current.Request.QueryString["thread"].ToString()); |
| string uName = Telerik.Security.UserManager.GetCurrentUserName(); |
| MembershipUser user = Membership.GetUser(uName); |
| Guid userID = new Guid(user.ProviderUserKey.ToString()); |
| string eMail = user.Email; |
| // subscribe this user |
| ForumSubscriptionDataContext dataContext = new ForumSubscriptionDataContext(); |
| ForumSubscriber newSubscriber = new ForumSubscriber(); |
| newSubscriber.ThreadID = ThreadID; |
| newSubscriber.UserID = userID; |
| newSubscriber.UserEmail = eMail; |
| dataContext.ForumSubscribers.InsertOnSubmit(newSubscriber); |
| dataContext.SubmitChanges(); |
| subscriptionMessage.Text = "<span style=background-color:Green;>YOU HAVE BEEN SUCCESSFULLY SUBSCRIBED</span>"; |
| } |
| } |
| } |
| |