📄 lists.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 Lists.
/// </summary>
public class Lists : Wrox.ThePhile.Web.PhilePage
{
protected System.Web.UI.WebControls.LinkButton AddNew;
protected System.Web.UI.WebControls.LinkButton CancelAddNew;
protected System.Web.UI.WebControls.DataGrid ListsGrid;
protected System.Web.UI.WebControls.TextBox EditListDescr;
protected System.Web.UI.WebControls.RequiredFieldValidator ValidateEditName;
protected System.Web.UI.WebControls.TextBox EditListName;
protected System.Web.UI.WebControls.Table TableNewList;
protected System.Web.UI.WebControls.TextBox NewListDescr;
protected System.Web.UI.WebControls.RequiredFieldValidator ValidateNewName;
protected System.Web.UI.WebControls.TextBox NewListName;
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.Label AddNewError;
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);
}
if (!Page.IsPostBack)
{
// bind the page's controls
BindGrid();
}
}
protected void BindGrid()
{
// get all the lists
DataView myDV = Business.List.GetLists().Tables[0].DefaultView;
// sort the data according to the SortExpression value
if ( ListsGrid.Attributes["SortExpression"] != null )
myDV.Sort = ListsGrid.Attributes["SortExpression"];
ListsGrid.DataSource = myDV;
ListsGrid.DataBind();
}
protected void ListsGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
ListsGrid.EditItemIndex = -1;
// set the SortExpression attribute that will be used to actually sort
// the data in the BindGrid method
ListsGrid.Attributes["SortExpression"] = e.SortExpression.ToString();
BindGrid();
}
protected void ListsGrid_Edit(object sender, DataGridCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
// start editing
ListsGrid.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
protected void ListsGrid_CancelEdit(object sender, DataGridCommandEventArgs e)
{
ListsGrid.EditItemIndex = -1;
BindGrid();
}
protected void ListsGrid_Update(object sender, DataGridCommandEventArgs e)
{
if (Page.IsValid)
{
// get the new values from the textboxes
string listName = ((TextBox)e.Item.FindControl("EditListName")).Text;
string listDescr = ((TextBox)e.Item.FindControl("EditListDescr")).Text;
int listID = (int)ListsGrid.DataKeys[e.Item.ItemIndex];
// update the values
Business.List list = new Business.List(listID);
list.Name = listName;
list.Description = listDescr;
list.Update();
ListsGrid.EditItemIndex = -1;
BindGrid();
}
}
protected void ListsGrid_Delete(object sender, DataGridCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
ListsGrid.EditItemIndex = -1;
// get the ID of this record and delete it
Business.List list = new Business.List((int)ListsGrid.DataKeys[e.Item.ItemIndex]);
list.Delete();
BindGrid();
}
protected void AddNew_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Business.List list = new Business.List();
// add the new record
if (list.Create(NewListName.Text, NewListDescr.Text) < 0)
{
// if the call to the Add method returned -1, it means that this list
// was already present, so show the label that tells this
AddNewError.Visible = true;
}
ShowAddNewControls(false);
BindGrid();
}
}
protected void CancelAddNew_Click(object sender, EventArgs e)
{
ShowAddNewControls(false);
}
protected void Create_Click(object sender, EventArgs e)
{
// show the textboxes and buttons for adding a new record
AddNewError.Visible = false;
ShowAddNewControls(true);
ListsGrid.EditItemIndex = -1;
BindGrid();
}
protected void ShowAddNewControls(bool ShowControls)
{
// show/hide the controls for adding a new record
NewListName.Text="";
NewListDescr.Text="";
AddNewControlsRow.Visible = ShowControls;
CreateNewRow.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 + -