📄 subscriptions.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
{
/// <summary>
/// Summary description for Subscriptions.
/// </summary>
public class Subscriptions : Wrox.ThePhile.Web.PhilePage
{
protected System.Web.UI.WebControls.DropDownList ListsDropDown;
protected System.Web.UI.WebControls.TextBox EditEmail;
protected System.Web.UI.WebControls.TextBox EditLastName;
protected System.Web.UI.WebControls.TextBox EditFirstName;
protected System.Web.UI.WebControls.CheckBox EditActive;
protected System.Web.UI.WebControls.DataGrid SubscrGrid;
protected System.Web.UI.WebControls.Table TableLists;
protected System.Web.UI.WebControls.Button SendEmail;
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 TableNewSubscriber;
protected System.Web.UI.WebControls.TextBox NewFirstName;
protected System.Web.UI.WebControls.TextBox NewLastName;
protected System.Web.UI.WebControls.TextBox NewEmail;
protected System.Web.UI.WebControls.CheckBox NewActive;
protected System.Web.UI.WebControls.Label NewDate;
protected System.Web.UI.WebControls.RequiredFieldValidator ValidateNewEmail;
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.Label NumSubscriptions;
protected System.Web.UI.WebControls.RadioButton ShowAll;
protected System.Web.UI.WebControls.RadioButton ShowActive;
protected System.Web.UI.WebControls.RadioButton ShowInactive;
protected void Page_Load(object sender, EventArgs e)
{
// check if the current user is allowed to administer lists/subscriptions
if (!Context.User.Identity.IsAuthenticated ||
!((PhilePrincipal)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 from QueryString
string listID = Request.Params["ListID"];
SubscrGrid.Attributes["ListID"] = listID;
if (!Page.IsPostBack)
{
// load all the avaible lists in the DropDown control
ListsDropDown.DataSource = Business.List.GetLists().Tables[0].DefaultView;
ListsDropDown.DataBind();
if (listID!=null)
{
// select the DropDown element according to the ListID
ListsDropDown.SelectedIndex = ListsDropDown.Items.IndexOf(
ListsDropDown.Items.FindByValue(listID));
}
// (re)bind the page's controls
BindGrid();
}
}
protected void BindGrid()
{
int listID;
// get the ListID value from the Grid's attributes
if ( SubscrGrid.Attributes["ListID"] != null )
listID = int.Parse(SubscrGrid.Attributes["ListID"]);
else
listID = int.Parse(ListsDropDown.SelectedItem.Value);
// get all the subscribers of this list
Business.List list = new Business.List(listID);
DataTable myDT = list.GetSubscriptions().Tables[0];
DataView myDV = myDT.DefaultView;
DataView myDVActive = myDT.DefaultView;
// sort the records according to the SortExpression value in the Grid's attributes
if ( SubscrGrid.Attributes["SortExpression"]!="" )
myDV.Sort = SubscrGrid.Attributes["SortExpression"];
// show only the active/inactive subscriptions, if specified in the options
if (ShowActive.Checked)
myDV.RowFilter = "Active = 1";
else if (ShowInactive.Checked)
myDV.RowFilter = "Active = 0";
SubscrGrid.DataSource = myDV;
SubscrGrid.DataBind();
// keep the active subscriptions only
myDVActive.RowFilter = "Active = 1";
// write the number of total subscriptions, and the number of active subscriptions
NumSubscriptions.Text = String.Format("<small>{0} subscriptions in total, {1} active</small>",
myDT.Rows.Count, myDVActive.Count);
}
protected void ListsDropDown_IndexChanged(object sender, EventArgs e)
{
// reload the page to show the records for the selected list
// This will also hide the controls to add a new subscriber if they were visible
Response.Redirect("Subscriptions.aspx?ListID=" + ListsDropDown.SelectedItem.Value);
}
protected void ShowSubscriptions_CheckedChanged(object sender, EventArgs e)
{
// filter the subscriptions for the grid
BindGrid();
}
protected void SendEmail_Click(object sender, EventArgs e)
{
// redirect to the page to send an Email, adding the ListId value to the QueryString
Response.Redirect("SendNewsletter.aspx?ListID=" + SubscrGrid.Attributes["ListID"]);
}
protected void SubscrGrid_PageChanged(Object sender, DataGridPageChangedEventArgs e)
{
// the DataGrid page is changed, so hide the controls for adding a new record
// if they were visible
AddNewError.Visible = false;
ShowAddNewControls(false);
// change the current page
SubscrGrid.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
protected void SubscrGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
SubscrGrid.EditItemIndex = -1;
// set the SortExpression attribute that will be used to actually sort
// the data in the BindGrid method
SubscrGrid.Attributes["SortExpression"] = e.SortExpression.ToString();
BindGrid();
}
protected void SubscrGrid_Edit(object sender, DataGridCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
// start the editing for this item
SubscrGrid.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
protected void SubscrGrid_CancelEdit(object sender, DataGridCommandEventArgs e)
{
SubscrGrid.EditItemIndex = -1;
BindGrid();
}
protected void SubscrGrid_Update(object sender, DataGridCommandEventArgs e)
{
if (Page.IsValid)
{
// get the new values for the textboxes
string firstName = ((TextBox)e.Item.FindControl("EditFirstName")).Text;
string lastName = ((TextBox)e.Item.FindControl("EditLastName")).Text;
string email = ((TextBox)e.Item.FindControl("EditEmail")).Text;
bool active = ((CheckBox)e.Item.FindControl("EditActive")).Checked;
int subscrID = (int)SubscrGrid.DataKeys[e.Item.ItemIndex];
// update the subscriber's data
Business.Subscription subscription = new Business.Subscription(subscrID);
subscription.FirstName = firstName;
subscription.LastName = lastName;
subscription.Email = email;
subscription.Active = active;
subscription.Update();
SubscrGrid.EditItemIndex = -1;
BindGrid();
}
}
protected void SubscrGrid_Delete(object sender, DataGridCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
SubscrGrid.EditItemIndex = -1;
// delete this subscriber (but not the user, if he has subscriptions to other lists)
Business.Subscription subscription = new Business.Subscription((int)SubscrGrid.DataKeys[e.Item.ItemIndex]);
subscription.Delete();
BindGrid();
}
protected void AddNew_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
// add the new subscriber for this list
Business.List list = new Business.List(int.Parse(ListsDropDown.SelectedItem.Value));
if (list.AddSubscription(NewFirstName.Text, NewLastName.Text,
NewEmail.Text, NewActive.Checked).ID < 0)
{
// if the call to the Add method returned -1, it means that this user
// was already subscribed to this list, so make visible the label that tells this
AddNewError.Visible = true;
}
// hide the controls for adding a new subscriber
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
AddNewError.Visible = false;
ShowAddNewControls(true);
NewDate.DataBind();
SubscrGrid.EditItemIndex = -1;
BindGrid();
}
protected void ShowAddNewControls(bool ShowControls)
{
// show or hide the controls for adding a new subscriber
NewFirstName.Text = "";
NewLastName.Text = "";
NewEmail.Text = "";
NewActive.Checked = true;
CreateNewRow.Visible = !ShowControls;
AddNewResultCell.Visible = !ShowControls;
AddNewControlsRow.Visible = ShowControls;
}
#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 + -