📄 news.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 Wrox.WebModules.Accounts.Business;
namespace Wrox.WebModules.NewsManager.Web
{
/// <summary>
/// Summary description for news.
/// </summary>
public class News : Wrox.ThePhile.Web.PhilePage
{
protected System.Web.UI.WebControls.DropDownList CatDropDown;
protected System.Web.UI.WebControls.TextBox EditTitle;
protected System.Web.UI.WebControls.TextBox EditBody;
protected System.Web.UI.WebControls.TextBox EditReleaseDate;
protected System.Web.UI.WebControls.TextBox EditExpireDate;
protected System.Web.UI.WebControls.CheckBox EditApproved;
protected System.Web.UI.WebControls.DataGrid NewsGrid;
protected System.Web.UI.WebControls.Table TableNews;
protected System.Web.UI.WebControls.LinkButton AddNew;
protected System.Web.UI.WebControls.LinkButton CancelAddNew;
protected System.Web.UI.WebControls.HyperLink AddCancel;
protected System.Web.UI.WebControls.Table TableAddNews;
protected System.Web.UI.WebControls.TextBox NewTitle;
protected System.Web.UI.WebControls.TextBox NewBody;
protected System.Web.UI.WebControls.TextBox NewReleaseDate;
protected System.Web.UI.WebControls.TextBox NewExpireDate;
protected System.Web.UI.WebControls.CheckBox NewApproved;
protected System.Web.UI.WebControls.Button Create;
protected System.Web.UI.WebControls.TableRow AddNewControlsRow;
protected System.Web.UI.WebControls.TableRow CreateNewRow;
protected System.Web.UI.WebControls.TableCell AddNewResultCell;
protected System.Web.UI.WebControls.Label AddNewError;
protected System.Web.UI.WebControls.RadioButton ShowApproved;
protected System.Web.UI.WebControls.RadioButton ShowPending;
protected System.Web.UI.WebControls.RadioButton ShowPast;
protected System.Web.UI.WebControls.RadioButton ShowCurrent;
protected System.Web.UI.WebControls.RadioButton ShowFuture;
protected System.Web.UI.WebControls.Label NewsPreview;
protected void Page_Load(object sender, EventArgs e)
{
// check if the current user is allowed to administer the news
if (!Context.User.Identity.IsAuthenticated ||
!((PhilePrincipal)Context.User).HasPermission((int)NewsManagerPermissions.AdministerNews))
{
// if not, redirect to the Login page
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
// get the CategoryID from the QueryString
string categoryID = Request.Params["CategoryID"];
NewsGrid.Attributes["CategoryID"] = categoryID;
if (!Page.IsPostBack)
{
// load all the available lists in the DropDown control
DataView myDV = Business.Category.GetCategories().Tables[0].DefaultView;
CatDropDown.DataSource = myDV;
CatDropDown.DataBind();
if (categoryID!=null)
{
// select the DropDown element according to the CategoryID
CatDropDown.SelectedIndex = CatDropDown.Items.IndexOf(
CatDropDown.Items.FindByValue(categoryID));
}
// (re)bind the page's controls
BindGrid();
}
}
protected void BindGrid()
{
int categoryID;
// get the CategoryID value from the Grid's attributes
if ( NewsGrid.Attributes["CategoryID"] != null )
categoryID = int.Parse(NewsGrid.Attributes["CategoryID"]);
else
categoryID = int.Parse(CatDropDown.SelectedItem.Value);
// get all the news of this category
DataView myDV = new Business.Category(categoryID).GetNews(false).Tables[0].DefaultView;
// sort the records according to the SortExpression value in the Grid's attributes
if ( NewsGrid.Attributes["SortExpression"]!="" )
myDV.Sort = NewsGrid.Attributes["SortExpression"];
// show only the approved/pending news, as specified in the options
if (ShowApproved.Checked)
myDV.RowFilter = "Approved = 1";
else if (ShowPending.Checked)
myDV.RowFilter = "Approved = 0";
else
myDV.RowFilter = "1 = 1";
// show only the past/current/future news, if specified in the options
if (ShowPast.Checked)
myDV.RowFilter += " AND ExpireDate < '" + DateTime.Today + "'";
else if (ShowCurrent.Checked)
myDV.RowFilter += " AND ReleaseDate <= '" + DateTime.Today + "' AND ExpireDate >= '" + DateTime.Today + "'";
else if (ShowFuture.Checked)
myDV.RowFilter += " AND ReleaseDate > '" + DateTime.Today + "'";
NewsGrid.DataSource = myDV;
NewsGrid.DataBind();
}
protected void CatDropDown_IndexChanged(object sender, EventArgs e)
{
// reload the page to show the news for the selected category
// This will also hide the controls to add a news if they were visible
Response.Redirect("News.aspx?CategoryID=" + CatDropDown.SelectedItem.Value);
}
protected void ShowNews_CheckedChanged(object sender, EventArgs e)
{
UnselectGridItem();
// filter the news for the grid
BindGrid();
}
protected void NewsGrid_PageChanged(Object sender, DataGridPageChangedEventArgs e)
{
// the DataGrid page is changed, so hide the controls for adding a new record
// if they were visible
UnselectGridItem();
AddNewError.Visible = false;
ShowAddNewControls(false);
// change the current page
NewsGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
protected void NewsGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
UnselectGridItem();
AddNewError.Visible = false;
ShowAddNewControls(false);
NewsGrid.EditItemIndex = -1;
// set the SortExpression attribute that will be used to actually sort
// the data in the BindGrid method
NewsGrid.Attributes["SortExpression"] = e.SortExpression.ToString();
BindGrid();
}
protected void NewsGrid_Edit(object sender, DataGridCommandEventArgs e)
{
UnselectGridItem();
AddNewError.Visible = false;
ShowAddNewControls(false);
// start the editing for this item
NewsGrid.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
protected void NewsGrid_CancelEdit(object sender, DataGridCommandEventArgs e)
{
NewsGrid.EditItemIndex = -1;
BindGrid();
}
protected void NewsGrid_Update(object sender, DataGridCommandEventArgs e)
{
if (Page.IsValid)
{
int newsID = (int)NewsGrid.DataKeys[e.Item.ItemIndex];
// get the new values for the textboxes
string title = ((TextBox)e.Item.FindControl("EditTitle")).Text;
string body = ((TextBox)e.Item.FindControl("EditBody")).Text;
bool approved = ((CheckBox)e.Item.FindControl("EditApproved")).Checked;
// set predefined release/expire dates, in case they are not specified
DateTime releaseDate = DateTime.Today;
DateTime expireDate = new DateTime(3000,1,1);
// get a reference to the textboxes with the dates
TextBox releaseDateTB = (TextBox)e.Item.FindControl("EditReleaseDate");
TextBox expireDateTB = (TextBox)e.Item.FindControl("EditExpireDate");
// if the release date is specified, take that, if not keep the predefined
if (releaseDateTB.Text.Trim().Length > 0)
releaseDate = DateTime.Parse(releaseDateTB.Text);
if (expireDateTB.Text.Trim().Length > 0)
expireDate = DateTime.Parse(expireDateTB.Text);
// update the news
Business.News news = new Business.News(newsID);
news.Title = title;
news.Body = body;
news.ReleaseDate = releaseDate;
news.ExpireDate = expireDate;
news.Approved = approved;
news.Update();
NewsGrid.EditItemIndex = -1;
BindGrid();
}
}
protected void NewsGrid_Delete(object sender, DataGridCommandEventArgs e)
{
UnselectGridItem();
AddNewError.Visible = false;
ShowAddNewControls(false);
NewsGrid.EditItemIndex = -1;
// delete this news
Business.News news = new Business.News((int)NewsGrid.DataKeys[e.Item.ItemIndex]);
news.Delete();
BindGrid();
}
protected void NewsGrid_SelectionChanged(object sender, EventArgs e)
{
ShowAddNewControls(false);
// get the ID of the selected row
int newsID = int.Parse(NewsGrid.DataKeys[NewsGrid.SelectedIndex].ToString());
// show the Body text in the Preview label
NewsPreview.Text = new Business.News(newsID).Body;
NewsPreview.Visible = true;
NewsGrid.EditItemIndex = -1;
BindGrid();
}
protected void AddNew_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
int categoryID = int.Parse(CatDropDown.SelectedItem.Value);
string title = NewTitle.Text;
string body = NewBody.Text;
bool approved = NewApproved.Checked;
// set predefined release/expire dates, in case they are not specified
DateTime releaseDate = DateTime.Today;
DateTime expireDate = new DateTime(3000,1,1);
// if the dates are supplied, take them, otherwise keep the predefined
if (NewReleaseDate.Text.Trim().Length > 0)
releaseDate = DateTime.Parse(NewReleaseDate.Text);
if (NewExpireDate.Text.Trim().Length > 0)
expireDate = DateTime.Parse(NewExpireDate.Text);
// add the news
Business.Category category = new Business.Category(categoryID);
SiteIdentity currUser = (SiteIdentity)Context.User.Identity;
if (category.AddNews(title, body, releaseDate, expireDate, approved, currUser.UserID).ID < 0)
{
// if the call to the Add method returned -1, it means that this news
// was already present, so show the label that tells this
AddNewError.Visible = true;
}
// hide the controls for adding a news
ShowAddNewControls(false);
BindGrid();
}
}
protected void CancelAddNew_Click(object sender, EventArgs e)
{
ShowAddNewControls(false);
}
protected void Create_Click(object sender, EventArgs e)
{
// show the controls for adding a new subscriber and unselect the selected item
UnselectGridItem();
AddNewError.Visible = false;
ShowAddNewControls(true);
NewsGrid.EditItemIndex = -1;
BindGrid();
}
protected void UnselectGridItem()
{
// hide the Preview control and unselect the selected row
NewsGrid.SelectedIndex = -1;
NewsPreview.Text = "";
NewsPreview.Visible = false;
}
protected void ShowAddNewControls(bool ShowControls)
{
// show or hide the controls for adding a new subscriber
NewTitle.Text = "";
NewBody.Text = "";
NewReleaseDate.Text = "";
NewExpireDate.Text = "";
NewApproved.Checked = true;
CreateNewRow.Visible = !ShowControls;
AddNewResultCell.Visible = !ShowControls;
AddNewControlsRow.Visible = ShowControls;
}
// return the complete body of the specified news
protected string GetNewsText(int newsID)
{
return new Business.News(newsID).Body;
}
#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 + -