📄 topic.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using infoWeb.WebModules.Accounts.Business;
namespace infoWeb.WebModules.Forums.Web
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class Topic : infoWeb.ThePhile.Web.PhilePage
{
protected System.Web.UI.WebControls.DataGrid RepliesGrid;
protected System.Web.UI.WebControls.Label PageNumber;
protected System.Web.UI.WebControls.Label TotalPages;
protected System.Web.UI.WebControls.LinkButton FirstPage;
protected System.Web.UI.WebControls.LinkButton PreviousPage;
protected System.Web.UI.WebControls.LinkButton NextPage;
protected System.Web.UI.WebControls.LinkButton LastPage;
protected System.Web.UI.WebControls.LinkButton DeleteTopic;
protected System.Web.UI.WebControls.HyperLink EditTopic;
protected System.Web.UI.HtmlControls.HtmlTable TopicTable;
protected System.Web.UI.WebControls.Label TopicAuthor;
protected System.Web.UI.WebControls.Label TopicSubject;
protected System.Web.UI.WebControls.Label TopicMessage;
protected System.Web.UI.WebControls.Label TopicAuthorSignature;
protected System.Web.UI.WebControls.Image TopicAuthorAvatar;
protected System.Web.UI.WebControls.HyperLink TopicAuthorHomepage;
protected System.Web.UI.WebControls.Label TopicDate;
protected System.Web.UI.WebControls.Label TopicTime;
protected System.Web.UI.WebControls.Label GridItemStyle;
protected System.Web.UI.WebControls.Label GridAlternatingItemStyle;
protected System.Web.UI.WebControls.HyperLink NewTopic;
protected System.Web.UI.WebControls.HyperLink NewReply;
protected Web.Controls.User.Header Header;
private bool canModerateForums;
protected void Page_Load(object sender, EventArgs e)
{
// check if the user has the permission to moderate the forums
canModerateForums = (Context.User.Identity.IsAuthenticated &&
((SitePrincipal)Context.User).HasPermission((int)ForumsPermissions.ModerateForums));
string topicID = "-1";
// get the ForumID from the QueryString
if (Request.QueryString["TopicID"] != null)
topicID = Request.Params["TopicID"];
// show or hide the Edit and Delete topic links, according to the permissions
// of the current user
EditTopic.NavigateUrl = "PostMessage.aspx?Action=EditTopic&TopicID=" + topicID.ToString();
EditTopic.Visible = canModerateForums;
DeleteTopic.Visible = canModerateForums;
if (!Page.IsPostBack)
{
Business.Topic topic;
// if the TopicKey value is not null, find the topic with that key and redirect to it
if (Request.QueryString["TopicKey"] != null)
{
topic = new Business.Topic(Request.QueryString["TopicKey"].ToString());
// if found, redirect to it, otherwise redirect to Default.aspx
if (topic.ID != -1)
Response.Redirect("Topic.aspx?TopicID=" + topic.ID.ToString(), true);
else
Response.Redirect("Default.aspx", true);
}
else // otherwise it means that we have a TopicID to use directly
{
// set the links for posting a new topic/reply
topic = new Business.Topic(int.Parse(topicID));
NewTopic.NavigateUrl += topic.Forum.ID.ToString();
NewReply.NavigateUrl += topic.ID.ToString();
// set the navigation bar TopicID
Header.TopicID = int.Parse(topicID);
// if there is the paramter idicating to show the last page, set the number
// in the label. This will be read in the BindGrid procedure
if (Request.QueryString["Display"] != null &&
Request.QueryString["Display"].ToString().ToLower() == "lastpage")
{
int pageSize = Configuration.ModuleConfig.GetSettings().RepliesPerPage;
int totalPages = (int)Math.Ceiling((double)topic.Replies/pageSize);
PageNumber.Text = totalPages.ToString();
}
// bind the data to the grid
RepliesGrid.Attributes["TopicID"] = topicID;
BindGrid();
}
}
}
public bool CanModerateForums
{
get { return canModerateForums; }
}
protected void BindGrid()
{
// get the ForumID value from the Grid's attributes.
// if null, redirect to the Default.aspx page
if (RepliesGrid.Attributes["TopicID"] == null)
Response.Redirect("Default.aspx", true);
// get the current forum's ID
int topicID = int.Parse(RepliesGrid.Attributes["TopicID"]);
// get the number of topics per page
int pageSize = Configuration.ModuleConfig.GetSettings().RepliesPerPage;
// get the current replies page
int pageNumber = (PageNumber.Text.Trim().Length == 0 ? 1 : int.Parse(PageNumber.Text));
// retrieve and bind the records to the grid
Business.Topic topic = new Business.Topic(topicID);
DataSet replies = topic.GetReplies(pageNumber);
RepliesGrid.DataSource = replies.Tables[0].DefaultView;
RepliesGrid.DataBind();
// show the total number of pages
int totalPages = (int)Math.Ceiling((double)topic.Replies/pageSize);
if (totalPages == 0) totalPages = 1;
TotalPages.Text = totalPages.ToString();
// enable/disable the links to navigate through the pages
FirstPage.Enabled = (pageNumber != 1);
PreviousPage.Enabled = (pageNumber != 1);
NextPage.Enabled = (pageNumber != totalPages);
LastPage.Enabled = (pageNumber != totalPages);
// show also the topic, if this is the first page
if (pageNumber == 1)
{
BindTopicControls(topicID);
TopicTable.Visible = true;
// if this is the first page, also swap the styles for the Item and AlternatingItem
// of the DataGrid, because the Topic has the Item style
RepliesGrid.ItemStyle.CssClass = GridAlternatingItemStyle.Text;
RepliesGrid.AlternatingItemStyle.CssClass = GridItemStyle.Text;
}
else
{
TopicTable.Visible = false;
RepliesGrid.ItemStyle.CssClass = GridItemStyle.Text;
RepliesGrid.AlternatingItemStyle.CssClass = GridAlternatingItemStyle.Text;
}
}
private void BindTopicControls(int topicID)
{
// retrieve all the topic's info
Business.Topic topic = new Business.Topic(topicID);
Business.Member author = topic.Member;
// show the data
TopicAuthor.Text = GetAuthorText(author.Name, author.Email, author.ShowEmail);
TopicAuthorAvatar.ImageUrl = author.AvatarUrl;
TopicAuthorAvatar.Visible = (author.AvatarUrl.Length > 0);
TopicAuthorHomepage.NavigateUrl = author.Homepage;
TopicAuthorHomepage.Visible = (author.Homepage.Length > 0);
TopicDate.Text = string.Format("{0:MM/dd/yy}", topic.AddedDate);
TopicTime.Text = string.Format("{0:HH:mm:ss tt}", topic.AddedDate);
TopicSubject.Text = topic.Subject;
TopicMessage.Text = Business.Helper.ProcessSpecialTags(topic.Message);
TopicAuthorSignature.Text = Business.Helper.ProcessSpecialTags(author.Signature);
}
public string GetAuthorText(object memberName, object eMail, object showEmail)
{
// return the member Name only or a link to its e-mail address, according to the ShowEmail value
if (!Convert.ToBoolean(showEmail))
return memberName.ToString();
else
return string.Format("<a href=\"mailto:{0}\">{1}</a>", eMail.ToString(), memberName.ToString());
}
public string ProcessTags(object rawText)
{
return Business.Helper.ProcessSpecialTags(rawText.ToString());
}
protected void RepliesGrid_Delete(object sender, DataGridCommandEventArgs e)
{
// if the user cannot moderate forums redirect to the login page
if (!canModerateForums)
{
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
// delete this reply
Business.Reply reply = new Business.Reply((int)RepliesGrid.DataKeys[e.Item.ItemIndex]);
reply.Delete();
BindGrid();
}
protected void DeleteTopic_Click(object sender, EventArgs e)
{
// if the user cannot moderate forums redirect to the login page
if (!canModerateForums)
{
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
int topicID = int.Parse(Request.QueryString["TopicID"]);
Business.Topic topic = new Business.Topic(topicID);
int forumID = topic.ForumID;
topic.Delete();
Response.Redirect("Forum.aspx?ForumID=" + forumID.ToString());
}
protected void RepliesGrid_PageChanged(object sender, CommandEventArgs e)
{
if (PageNumber.Text.Trim().Length == 0) return;
switch (e.CommandName)
{
case "FirstPage":
PageNumber.Text = "1";
break;
case "PreviousPage":
PageNumber.Text = (int.Parse(PageNumber.Text) -1).ToString();
break;
case "NextPage":
PageNumber.Text = (int.Parse(PageNumber.Text) +1).ToString();
break;
case "LastPage":
PageNumber.Text = TotalPages.Text;
break;
}
// show the new page
BindGrid();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
base.OnInit(e);
InitializeComponent();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -