📄 categories.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 Categories.
/// </summary>
public class Categories : Wrox.ThePhile.Web.PhilePage
{
protected System.Web.UI.WebControls.LinkButton AddNew;
protected System.Web.UI.WebControls.LinkButton CancelAddNew;
protected System.Web.UI.WebControls.DataGrid CatGrid;
protected System.Web.UI.WebControls.Table TableNewCategory;
protected System.Web.UI.WebControls.RequiredFieldValidator ValidateEditName;
protected System.Web.UI.WebControls.RequiredFieldValidator ValidateNewName;
protected System.Web.UI.WebControls.TextBox EditCatName;
protected System.Web.UI.WebControls.TextBox EditCatDescr;
protected System.Web.UI.WebControls.TextBox EditCatImageUrl;
protected System.Web.UI.WebControls.TextBox NewCatName;
protected System.Web.UI.WebControls.TextBox NewCatDescr;
protected System.Web.UI.WebControls.TextBox NewCatImageUrl;
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 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);
}
if (!Page.IsPostBack)
{
// bind the page's controls
BindGrid();
}
}
protected void BindGrid()
{
// get all the categories
DataView myDV = Business.Category.GetCategories().Tables[0].DefaultView;
// sort the data according to the SortExpression value
if (CatGrid.Attributes["SortExpression"] != null)
myDV.Sort = CatGrid.Attributes["SortExpression"];
CatGrid.DataSource = myDV;
CatGrid.DataBind();
}
protected void CatGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
CatGrid.EditItemIndex = -1;
// set the SortExpression attribute that will be used to actually sort
// the data in the BindGrid method
CatGrid.Attributes["SortExpression"] = e.SortExpression.ToString();
BindGrid();
}
protected void CatGrid_Edit(object sender, DataGridCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
// start editing
CatGrid.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
protected void CatGrid_CancelEdit(object sender, DataGridCommandEventArgs e)
{
CatGrid.EditItemIndex = -1;
BindGrid();
}
protected void CatGrid_Update(object sender, DataGridCommandEventArgs e)
{
if (Page.IsValid)
{
// get the new values from the textboxes
string catName = ((TextBox)e.Item.FindControl("EditCatName")).Text;
string catDescr = ((TextBox)e.Item.FindControl("EditCatDescr")).Text;
string catImageUrl = ((TextBox)e.Item.FindControl("EditCatImageUrl")).Text;
int categoryID = (int)CatGrid.DataKeys[e.Item.ItemIndex];
// update the values
Business.Category category = new Business.Category(categoryID);
category.Name = catName;
category.Description = catDescr;
category.ImageUrl = catImageUrl;
category.Update();
CatGrid.EditItemIndex = -1;
BindGrid();
}
}
protected void CatGrid_Delete(object sender, DataGridCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
CatGrid.EditItemIndex = -1;
// get the ID of this record and delete it
Business.Category category = new Business.Category((int)CatGrid.DataKeys[e.Item.ItemIndex]);
category.Delete();
// re-bind
BindGrid();
}
protected void AddNew_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Business.Category category = new Business.Category();
// add the new record
if (category.Create(NewCatName.Text, NewCatDescr.Text, NewCatImageUrl.Text) < 0)
{
// if Add returned -1, the category was already present
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);
CatGrid.EditItemIndex = -1;
BindGrid();
}
protected void ShowAddNewControls(bool ShowControls)
{
// show/hide the controls for adding a new record
NewCatName.Text="";
NewCatDescr.Text="";
NewCatImageUrl.Text="";
AddNewControlsRow.Visible = ShowControls;
CreateNewRow.Visible = !ShowControls;
}
public string GetImage(object imageUrl)
{
if (imageUrl.ToString().Length > 0)
return "<img border=\"0\" src=\"" + imageUrl.ToString() + "\">";
else
return "";
}
#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 + -