else myLabel.Text = \你无权删除此新闻\; } }
protected void LinkButton1_Click1(object sender, EventArgs e) {
if (Page.IsValid) {
if (search.SelectedIndex == 0) {
searchTitle(); }
else searchContent(); } } }
4.4 分类管理模块
分类管理模块主要有分类的添加、删除和修改的功能。该系统中,只有系统管理员具有该权限。
其功能界面如图4-4-1所示:
图 4-4-1 分类管理界面图
其部分代码如下:
public partial class admin_admin_class : System.Web.UI.Page
{
DataRow dr;
string con = ConfigurationManager.ConnectionStrings[\].ConnectionString; public DataSet ds = new DataSet(); public string oldclass;
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
string userclass;
userclass = (string)Session[\]; if (userclass.Trim() == \系统管理员\) {
getClass(); }
else Page.Visible = false; } }
protected void getClass() { try {
string
con
=
ConfigurationManager.ConnectionStrings[\].ConnectionString; SqlConnection conn = new SqlConnection(con);//连接字符串 conn.Open();
SqlDataAdapter myCommand = new SqlDataAdapter(); //创建SqlDataAdapter 类 myCommand.SelectCommand = new SqlCommand(\, conn); myCommand.SelectCommand.CommandType = CommandType.StoredProcedure; myCommand.Fill(ds, \); MyDataGrid.DataSource = ds; MyDataGrid.DataBind();
lblCurrentIndex.Text = \第\ + ((Int32)MyDataGrid.CurrentPageIndex + 1) + \页\;
lblPageCount.Text = \共\ + MyDataGrid.PageCount + \页\; // // }
catch (SqlException e) {
Console.WriteLine(\ + e.Message); //出错处理 } }
MyDataGrid.DataSource=ds; MyDataGrid.DataBind();
//绑定
//得到头条新闻
conn.Close();
protected void updateArticle(string a, string b) 的文章分类名改成现有分类 { try {
//把所有具有原分类
SqlConnection conn = new SqlConnection(con);//连接字符串 conn.Open();
SqlCommand cmd = new SqlCommand(\, conn); cmd.CommandType = CommandType.StoredProcedure;
SqlParameter ce = cmd.Parameters.Add(\, SqlDbType.Char, 200); ce.Value = a.Trim();
SqlParameter oce = cmd.Parameters.Add(\, SqlDbType.Char, 200); oce.Value = b.Trim();
int i = cmd.ExecuteNonQuery(); if (i > 0) { }
conn.Close(); }
catch (SqlException e) {
Console.WriteLine(\ + e.Message); //出错处理 } }
protected void delClass(object d) { try {
SqlConnection conn = new SqlConnection(con);//连接字符串 conn.Open();
SqlCommand delClass = new SqlCommand(\, conn); delClass.CommandType = CommandType.StoredProcedure;
SqlParameter cl = delClass.Parameters.Add(\, SqlDbType.BigInt); cl.Value = d;
int i = delClass.ExecuteNonQuery(); if (i > 0) {
myLabel.Text = \删除\ + \成功\; }
else myLabel.Text = \删除\ + \失败\; conn.Close(); }
catch (SqlException er) {
//删除分类
Console.WriteLine(\ + er.Message); //出错处理 } }
public string show(object a) {
string b = a.ToString(); return b; }
public void PagerButtonClick(Object sender, EventArgs e) {
//获得LinkButton的参数值
string arg = ((LinkButton)sender).CommandArgument;
switch (arg) {
case (\):
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1)) MyDataGrid.CurrentPageIndex++; break; case (\):
if (MyDataGrid.CurrentPageIndex > 0) MyDataGrid.CurrentPageIndex--; break; case (\):
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1); break; case (\):
MyDataGrid.CurrentPageIndex = 0; break; }
getClass(); }
#region Web Form Designer generated code override protected void OnInit(EventArgs e) {
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 InitializeComponent(); base.OnInit(e); }
protected void InitializeComponent() {
this.Load += new System.EventHandler(this.Page_Load); }
public {
void MyDataGrid_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e) if (Page.IsValid) { try {
SqlConnection myConnection = new SqlConnection(con);//连接字符串 myConnection.Open();
TextBox classname = (TextBox)e.Item.Cells[1].Controls[1]//取得TextBox中的控件 string clne = classname.Text.Trim();
string oldname = (string)Session[\]; if (clne == oldname) {
myLabel.Text = \你分类名没改,没必要修改!\//分类没改,则不必修改 } else {
SqlCommand myConnection);
validateClass.CommandType = CommandType.StoredProcedure; SqlParameter SqlDbType.Char, 200);
cl.Value = classname.Text;
SqlDataReader validateReader = validateClass.ExecuteReader(); if (validateReader.Read() == true) {
myLabel.Text = \已有此分类名,请重新输入一个!!\; validateReader.Close(); myConnection.Close(); } else { try {
validateReader.Close(); myConnection.Close();
SqlConnection cont = new SqlConnection(con); cont.Open();
SqlCommand updateClass = new SqlCommand(\, cont); //通过存储过程更新
updateClass.CommandType = CommandType.StoredProcedure; SqlParameter sd = updateClass.Parameters.Add(\, SqlDbType.Char, 200);
//如分类名已有,重输
cl
=
validateClass.Parameters.Add(\,
validateClass
=
new
SqlCommand(\,