📄 admin_remark.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 System.Data.SqlClient;
using System.Configuration;
namespace WebNews
{
/// <summary>
/// admin_remark 的摘要说明。
/// </summary>
public class admin_remark : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label myLabel;
protected System.Web.UI.WebControls.TextBox keyword;
protected System.Web.UI.WebControls.DropDownList search;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.Label PageList;
protected System.Web.UI.WebControls.ImageButton ImageButton1;
protected System.Web.UI.WebControls.LinkButton btnFirst;
protected System.Web.UI.WebControls.LinkButton LinkButton2;
protected System.Web.UI.WebControls.LinkButton Linkbutton3;
protected System.Web.UI.WebControls.LinkButton Linkbutton4;
protected System.Web.UI.WebControls.Label lblCurrentIndex;
protected System.Web.UI.WebControls.Label lblPageCount;
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
string dr=(string)Session["userclass"];
string a= (string)Session["classname"];
string b=Request["classname"];
if(dr.Trim()=="系统管理员")
{
getRemark();
}
else
{
if((int)Session["remark"]==1 &&a.Trim()==b.Trim())
{
getRemark();
}
else myLabel.Text="你无权管理评论";
}
}
}
private void getRemark() //获得评论
{
try
{
string con=ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn = new SqlConnection(con);//连接字符串
conn.Open();
SqlDataAdapter myCommand = new SqlDataAdapter(); //创建SqlDataAdapter 类
myCommand.SelectCommand=new SqlCommand("sp_selRemarkByArticleId",conn);
myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
SqlParameter artid=myCommand.SelectCommand.Parameters.Add("@articleid",SqlDbType.BigInt);
artid.Value=Request["articleid"];
DataSet ds=new DataSet(); //建立并填充数据集
myCommand.Fill(ds,"Article");
MyDataGrid.DataSource=ds;
MyDataGrid.DataBind();
lblCurrentIndex.Text="第"+((Int32)MyDataGrid.CurrentPageIndex+1)+"页";
lblPageCount.Text="/共"+MyDataGrid.PageCount+"页";
conn.Close();
}
catch(SqlException e)
{
Response.Write("Exception in Main: " + e.Message); //出错处理
}
}
private void delRemark(object a)
{
try
{ string conn=ConfigurationSettings.AppSettings["dsn"];
SqlConnection con = new SqlConnection(conn);//连接字符串
con.Open();
SqlCommand cmd=new SqlCommand("sp_delRemarkById",con); //建立命令
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter rid=cmd.Parameters.Add("@id",SqlDbType.BigInt ); //调用并设置存储过程参数
rid.Value =a;
int i=cmd.ExecuteNonQuery();
if(i>0)
{
myLabel.Text="删除成功";
}
con.Close();
}
catch(SqlException e)
{
Response.Write("Exception in Main: " + e.Message); //出错处理
}
}
private void searchBody()
{
try
{
string con=ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn = new SqlConnection(con);//连接字符串
conn.Open();
SqlDataAdapter myCommand = new SqlDataAdapter(); //创建SqlDataAdapter 类
myCommand.SelectCommand=new SqlCommand("sp_searchRemarkByBody",conn);
myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
SqlParameter body=myCommand.SelectCommand.Parameters.Add("@body",SqlDbType.NVarChar ,1000);
body.Value=Request["keyword"] ;
DataSet ds=new DataSet(); //建立并填充数据集
myCommand.Fill(ds,"Articl");
MyDataGrid.DataSource=ds; //绑定
MyDataGrid.DataBind();
conn.Close();
}
catch(SqlException e)
{
Response.Write("Exception in Main: " + e.Message); //出错处理
}
}
private void searchAuthor()
{
try
{
string con=ConfigurationSettings.AppSettings["dsn"];
SqlConnection conn = new SqlConnection(con);//连接字符串
conn.Open();
SqlDataAdapter myCommand = new SqlDataAdapter(); //创建SqlDataAdapter 类
myCommand.SelectCommand=new SqlCommand("sp_searchRemarkByAuthor",conn);
myCommand.SelectCommand.CommandType=CommandType.StoredProcedure ;
SqlParameter username=myCommand.SelectCommand.Parameters.Add("@username",SqlDbType.NVarChar ,1000);
username.Value=Request["keyword"] ;
DataSet ds=new DataSet(); //建立并填充数据集
myCommand.Fill(ds,"Article");
MyDataGrid.DataSource=ds; //绑定
MyDataGrid.DataBind();
conn.Close();
}
catch(SqlException e)
{
Response.Write("Exception in Main: " + e.Message); //出错处理
}
}
public void PagerButtonClick(Object sender, EventArgs e)
{
//获得LinkButton的参数值
string arg = ((LinkButton)sender).CommandArgument;
switch(arg)
{
case ("next"):
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
MyDataGrid.CurrentPageIndex ++;
break;
case ("prev"):
if (MyDataGrid.CurrentPageIndex > 0)
MyDataGrid.CurrentPageIndex --;
break;
case ("last"):
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
case("first"):
MyDataGrid.CurrentPageIndex =0;
break;
}
getRemark();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
this.MyDataGrid.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_DeleteCommand);
this.MyDataGrid.SelectedIndexChanged += new System.EventHandler(this.MyDataGrid_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void MyDataGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
object b=this.MyDataGrid.DataKeys[e.Item.ItemIndex];
delRemark(b);
if(MyDataGrid.CurrentPageIndex>0)
{
MyDataGrid.CurrentPageIndex=MyDataGrid.CurrentPageIndex-1;
}
getRemark();
}
private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(Page.IsValid)
{
if(search.SelectedIndex==0)
{
searchBody();
}
else searchAuthor();
}
}
public void MyDataGrid_SelectedIndexChanged(object sender,System.EventArgs e)
{
getRemark();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -