📄 dp_check1.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class DP_Check1 : System.Web.UI.Page
{
public static int TotalPage;//定义变量来保存总页数
public int CurPage;//定义变量来保存当前页索引
public int Tnum;//总条数;
public int EachPage;//每页总条数
public string username;
public string fangshi;
public string n;
public string productstore;
protected void Page_Load(object sender, EventArgs e)
{
Session["username"] = "somboy";
if (Session["username"] == null)
{
Response.Write("<script language='JavaScript'>");
Response.Write("alert('请先登录!')");
Response.Write("</script>");
Response.End();
return;
}
n = "1";
if (!Page.IsPostBack)
{
gv_DataBind();
}
}
public void gv_DataBind()
{
productstore = Session["username"].ToString();
SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
string sql = "select * from Products where ProductState=" + n + " And UserName='" + productstore + "'";
/*
SqlCommand command=new SqlCommand(sql,connection);
connection.Open();
SqlDataReader dr=command.ExecuteReader();
*/
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
da.Fill(ds, "Table");
PagedDataSource objPage = new PagedDataSource();//创建分页类
objPage.DataSource = ds.Tables["Table"].DefaultView;//设置数据源
objPage.AllowPaging = true;
objPage.PageSize = 10;
if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
CurPage = Math.Min(CurPage, objPage.PageCount);
CurPage = Math.Max(CurPage, 1);
}
else
CurPage = 1;
objPage.CurrentPageIndex = CurPage - 1;
TotalPage = objPage.PageCount;
Tnum = objPage.DataSourceCount;
EachPage = objPage.Count;
lblCurPage.Text = "第 " + CurPage.ToString() + " / " + TotalPage.ToString() + " 页";
lblTnum.Text = "共: " + Tnum + " 条记录";
lblEachPage.Text = "每页有: " + EachPage.ToString() + " 条记录";
if (objPage.CurrentPageIndex != 0)
lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1);
if (objPage.CurrentPageIndex != TotalPage - 1)
lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(TotalPage);
if (!objPage.IsFirstPage)
lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
if (!objPage.IsLastPage)
lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
DataGrid1.DataSource = objPage;
DataGrid1.DataBind();
}
private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
}
protected void DataGrid1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
{
string id = e.Item.Cells[0].Text;
productstore = Session["username"].ToString();
string sql = "delete from Products where ProductId=" + id;
SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
command.ExecuteNonQuery();
sql = "select * from Products where ProductState=" + n + " And UserName='" + productstore + "'";
command = new SqlCommand(sql, connection);
SqlDataReader dr = command.ExecuteReader();
DataGrid1.DataSource = dr;
DataGrid1.DataBind();
}
public void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
string id = e.Item.Cells[0].Text;
productstore = Session["username"].ToString();
string sql = "update Products set ProductState=0 where ProductId=" + id;
SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
command.ExecuteNonQuery();
sql = "select * from Products where ProductState=" + n + " And UserName='" + productstore + "'";
command = new SqlCommand(sql, connection);
SqlDataReader dr = command.ExecuteReader();
DataGrid1.DataSource = dr;
DataGrid1.DataBind();
}
public void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
string id = e.Item.Cells[0].Text;
productstore = Session["username"].ToString();
string sql = "select * from Products where ProductId=" + id;
SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
sql = "select * from Products where ProductState=" + n + " And UserName='" + productstore + "'";
command = new SqlCommand(sql, connection);
SqlDataReader dr = command.ExecuteReader();
DataGrid1.DataSource = dr;
DataGrid1.DataBind();
}
public void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
string id = e.Item.Cells[0].Text;
string sql = "select * from Products where ProductId=" + id;
SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.Read())
{
Response.Redirect("product_modify.aspx?id=" + id + "&msg=" + Server.UrlEncode("DP_Check1.aspx"));
}
}
protected void Button1_Click(object sender, EventArgs e)
{
username = TextBox1.Text;
if (username.Equals(""))
{
Response.Write("<script language='JavaScript'>");
Response.Write("alert('请输入关键字')");
Response.Write("</script>");
}
else
{
productstore = Session["username"].ToString();
SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnStr"]);
string sql = "Select * From Products Where ProductName Like '%" + username + "%' And ProductState=" + n + " And ProductStore='" + productstore + "'";
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if (!dr.Read())
{
Response.Write("<script language='JavaScript'>");
Response.Write("alert('!!该记录不存在!!')");
Response.Write("</script>");
return;
}
dr.Close();
sql = "Select * From Products Where ProductName Like '%" + username + "%' And ProductState=" + n + " And ProductStore='" + productstore + "'";
command = new SqlCommand(sql, connection);
dr = command.ExecuteReader();
DataGrid1.DataSource = dr;
DataGrid1.DataBind();
dr.Close();
connection.Close();
}
}
protected void btnPage_Click(object sender, EventArgs e)
{
int PageNum = 0;
if (!Request.Form["txtPage"].Equals(""))
PageNum = Convert.ToInt32(Request.Form["txtPage"]);
if (PageNum <= 0 || PageNum > TotalPage)
Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1));
else
Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(PageNum));
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -