📄 default.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 _Default : infoWeb.ThePhile.Web.PhilePage
{
protected System.Web.UI.WebControls.DataList CategoriesList;
protected System.Web.UI.WebControls.TextBox ForumName;
protected System.Web.UI.WebControls.TextBox ForumDescription;
protected System.Web.UI.WebControls.TextBox ForumCategoryCurr;
protected System.Web.UI.WebControls.TextBox ForumPosition;
protected System.Web.UI.WebControls.TextBox CategoryName;
protected System.Web.UI.WebControls.TextBox CategoryImageUrl;
protected System.Web.UI.WebControls.TextBox CategoryPosition;
protected System.Web.UI.WebControls.TextBox ForumIDCurr;
protected System.Web.UI.WebControls.TextBox CategoryIDCurr;
protected System.Web.UI.WebControls.Table ForumBox;
protected System.Web.UI.WebControls.Table CategoryBox;
protected System.Web.UI.WebControls.TableCell ForumBoxHeader;
protected System.Web.UI.WebControls.TableCell CategoryBoxHeader;
protected System.Web.UI.WebControls.TableRow ForumCategoryRow;
protected System.Web.UI.WebControls.DropDownList ForumCategory;
protected System.Web.UI.HtmlControls.HtmlInputHidden paramID;
protected System.Web.UI.WebControls.LinkButton SubmitForum;
protected System.Web.UI.WebControls.LinkButton CencelForum;
protected System.Web.UI.WebControls.LinkButton DeleteCategory;
protected System.Web.UI.WebControls.LinkButton DeleteForum;
private bool canAdministerCategories;
private void Page_Load(object sender, System.EventArgs e)
{
// do not allow to manage categories if the user is not authenticated or
// does not have the proper permission
canAdministerCategories = (Context.User.Identity.IsAuthenticated &&
((SitePrincipal)Context.User).HasPermission((int)ForumsPermissions.AdministerCategories));
if (!IsPostBack)
{
BindList();
}
}
public bool CanAdministerCategories
{
get { return canAdministerCategories; }
}
private void BindList()
{
// bind a DataView with all the categories to the DataList
CategoriesList.DataSource = Business.Category.GetCategories().Tables[0].DefaultView;
CategoriesList.DataBind();
}
public DataView GetForumsSource(int categoryID)
{
// get the DataView with the forums for the specified category
return new Business.Category(categoryID).GetForums().Tables[0].DefaultView;
}
protected void ForumsGrid_Edit(object sender, DataGridCommandEventArgs e)
{
// if the user cannot administer categories/forums redirect to the login page
if (!canAdministerCategories)
{
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
// get a reference to the clicked forum's parent grid
DataGrid forumsGrid = (DataGrid)e.Item.Parent.Parent;
// get the ID of the clicked forum
int forumID = (int)forumsGrid.DataKeys[e.Item.ItemIndex];
// get the forum's details
Business.Forum forum = new Business.Forum(forumID);
// set the controls for editing the record
ForumName.Text = forum.Name;
ForumDescription.Text = forum.Description;
ForumCategoryCurr.Text = forum.CategoryID.ToString();
ForumPosition.Text = forum.Position.ToString();
ForumIDCurr.Text = forum.ID.ToString();
ForumBoxHeader.Text = "Edit forum";
// hide the category box, if visible
CategoryBox.Visible = false;
// show the box, but hide the dropdownlist for the categories
ForumCategoryRow.Visible = false;
ForumBox.Visible = true;
}
// show the box for adding a forum
protected void NewForum_Click(object sender, EventArgs e)
{
// if the user cannot administer categories/forums redirect to the login page
if (!canAdministerCategories)
{
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
// hide the category box, if visible
CategoryBox.Visible = false;
ForumCategory.DataSource = Business.Category.GetCategories().Tables[0].DefaultView;
ForumCategory.DataBind();
// set the controls for the forum box
ForumBoxHeader.Text = "Create forum";
ForumPosition.Text = "";
ForumName.Text = "";
ForumDescription.Text = "";
ForumCategoryCurr.Text = "";
ForumIDCurr.Text = "";
ForumCategoryRow.Visible = true;
ForumBox.Visible = true;
}
// submit the box for adding/editing a forum
protected void SubmitForum_Click(object sender, EventArgs e)
{
// if the user cannot administer categories/forums redirect to the login page
if (!canAdministerCategories)
{
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
int forumPos = (ForumPosition.Text.Length > 0 ? int.Parse(ForumPosition.Text) : 0);
// if the hidden textbox is empty, it means that we're adding a forum
if (ForumIDCurr.Text.Length == 0)
{
// add a new forum
Business.Category category = new Business.Category(int.Parse(ForumCategory.SelectedItem.Value));
category.AddForum(ForumName.Text, ForumDescription.Text, forumPos);
}
else
{
// edit a forum
Business.Forum forum = new Business.Forum(int.Parse(ForumIDCurr.Text));
forum.Name = ForumName.Text;
forum.Description = ForumDescription.Text;
forum.Position = forumPos;
forum.Update();
}
// hide the forum box
ForumBox.Visible = false;
// refresh
BindList();
}
// cancel the box for adding/editing a forum
protected void CancelForum_Click(object sender, EventArgs e)
{
ForumBox.Visible = false;
}
protected void DeleteForum_Click(object sender, EventArgs e)
{
// if the user cannot administer categories/forums...
if (!canAdministerCategories)
{
// redirect to the Login page
// this is required because a hacker could manually call the DeleteForum javascript
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
// extract the ID of the forum to delete
int forumID = int.Parse(paramID.Value.ToString());
// delete the forum
Business.Forum forum = new Business.Forum(forumID);
forum.Delete();
// re-bind the list to show the updated records
BindList();
}
protected void CategoriesList_Edit(object sender, DataListCommandEventArgs e)
{
// if the user cannot administer categories/forums redirect to the login page
if (!canAdministerCategories)
{
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
// get the ID of the clicked category
int categoryID = (int)CategoriesList.DataKeys[e.Item.ItemIndex];
// get the category's details
Business.Category category = new Business.Category(categoryID);
// set the controls for editing the record
CategoryName.Text = category.Name;
CategoryImageUrl.Text = category.ImageUrl;
CategoryPosition.Text = category.Position.ToString();
CategoryIDCurr.Text = category.ID.ToString();
CategoryBoxHeader.Text = "Edit category";
// hide the forum box, if visible
ForumBox.Visible = false;
// show the category box
CategoryBox.Visible = true;
}
// show the box for adding a category
protected void NewCategory_Click(object sender, EventArgs e)
{
// if the user cannot administer categories/forums redirect to the login page
if (!canAdministerCategories)
{
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
// hide the forum box, if visible
ForumBox.Visible = false;
// set the controls for the category box
CategoryBoxHeader.Text = "Create category";
CategoryName.Text = "";
CategoryImageUrl.Text = "";
CategoryPosition.Text = "";
CategoryIDCurr.Text = "";
CategoryBox.Visible = true;
}
// submit the box for adding/editing a category
protected void SubmitCategory_Click(object sender, EventArgs e)
{
// if the user cannot administer categories/forums redirect to the login page
if (!canAdministerCategories)
{
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
int categoryPos = (CategoryPosition.Text.Length > 0 ? int.Parse(CategoryPosition.Text) : 0);
// if the hidden textbox is empty, it means that we're adding a category
if (CategoryIDCurr.Text.Length == 0)
{
// add a new category
Business.Category category = new Business.Category();
category.Create(CategoryName.Text, CategoryImageUrl.Text, categoryPos);
}
else
{
// edit a category
Business.Category category = new Business.Category(int.Parse(CategoryIDCurr.Text));
category.Name = CategoryName.Text;
category.ImageUrl = CategoryImageUrl.Text;
category.Position = categoryPos;
category.Update();
}
// hide the category box
CategoryBox.Visible = false;
// refresh
BindList();
}
// cancel the box for adding/editing a category
protected void CancelCategory_Click(object sender, EventArgs e)
{
CategoryBox.Visible = false;
}
protected void DeleteCategory_Click(object sender, EventArgs e)
{
// if the user cannot administer categories/forums...
if (!canAdministerCategories)
{
// redirect to the Login page
// this is required because a hacker could manually call the DeleteCategory javascript
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
// extract the ID of the category to delete
int categoryID = int.Parse(paramID.Value.ToString());
// delete the category
Business.Category category = new Business.Category(categoryID);
category.Delete();
// re-bind the list to show the updated records
BindList();
}
#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 + -