📄 newshistory.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.MailingLists.Web
{
public class NewsHistory : Wrox.ThePhile.Web.PhilePage
{
protected System.Web.UI.WebControls.Label MsgPreview;
protected System.Web.UI.WebControls.DataGrid NewsGrid;
protected System.Web.UI.WebControls.Table TableLists;
protected System.Web.UI.WebControls.DropDownList ListsDropDown;
protected void Page_Load(object sender, EventArgs e)
{
// check if the current user is allowed to administer lists/subscriptions
if (!Context.User.Identity.IsAuthenticated ||
!((SitePrincipal)Context.User).HasPermission((int)MailingListsPermissions.AdministerData))
{
// if not, redirect to the Login page
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
// get the ListID param from QueryString
string listID = Request.Params["ListID"];
NewsGrid.Attributes["ListID"] = listID;
if (!Page.IsPostBack)
{
// fill the DropDown control with the avaible lists
ListsDropDown.DataSource = Business.List.GetLists().Tables[0].DefaultView;
ListsDropDown.DataBind();
// select a DropDown item according to the ListID value specified in the QueryString
if (listID!=null)
{
ListsDropDown.SelectedIndex = ListsDropDown.Items.IndexOf(
ListsDropDown.Items.FindByValue(listID));
}
}
// (re)bind the page's controls
BindGrid();
}
protected void BindGrid()
{
int listID;
// get the ListID from the DataGrid's attribute
if ( NewsGrid.Attributes["ListID"] != null )
listID = int.Parse(NewsGrid.Attributes["ListID"]);
else
listID = int.Parse(ListsDropDown.SelectedItem.Value);
// get all the subscribers of that list
Business.List list = new Business.List(listID);
DataView myDV = list.GetNewsletters().Tables[0].DefaultView;
// sort the subscribers according to the specified DataGrid's attribute
if ( NewsGrid.Attributes["SortExpression"]!="" )
myDV.Sort = NewsGrid.Attributes["SortExpression"];
NewsGrid.DataSource = myDV;
NewsGrid.DataBind();
}
protected void ListsDropDown_IndexChanged(object sender, EventArgs e)
{
Response.Redirect("NewsHistory.aspx?ListID=" + ListsDropDown.SelectedItem.Value);
}
protected void NewsGrid_PageChanged(Object sender, DataGridPageChangedEventArgs e)
{
UnselectGridItem();
// change the current page
NewsGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
protected void NewsGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
UnselectGridItem();
// 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_Delete(object sender, DataGridCommandEventArgs e)
{
UnselectGridItem();
// delete the news identified by the ID of this row
Business.Newsletter newsletter = new Business.Newsletter((int)NewsGrid.DataKeys[e.Item.ItemIndex]);
newsletter.Delete();
BindGrid();
}
protected void NewsGrid_SelectionChanged(object sender, EventArgs e)
{
// get the ID of the selected row and the respective Newsletter object
int newsID = int.Parse(NewsGrid.DataKeys[NewsGrid.SelectedIndex].ToString());
Business.Newsletter newsletter = new Business.Newsletter(newsID);
// get the Body of the news from the DB
string body = newsletter.Body;
// if the news format was not HTML, encode the Body text
if (!newsletter.IsHTML)
{
body = HttpUtility.HtmlEncode(body);
// replace the new lines with <br>, to show the new lines in the Label
body = body.Replace("\n", "<br>");
body = body.Replace("\r", "");
}
// show the Body text in the Preview label
MsgPreview.Text = body;
MsgPreview.Visible = true;
}
protected void UnselectGridItem()
{
// hide the Preview control and unselect the selected row
NewsGrid.SelectedIndex = -1;
MsgPreview.Text = "";
MsgPreview.Visible = false;
}
#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 + -