categorymanage.aspx.cs
来自「microsoft公司在进行ASP.NET 2.0培训课时课堂操作演示代码。」· CS 代码 · 共 147 行
CS
147 行
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 Admins_CategoryManage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
this.GetCategoryDataBind();
}
}
private void Page_PreInit()
{
if(Request.QueryString["theme"]!=null)
{
switch(Request.QueryString["theme"])
{
case "Default":
Profile.MyTheme = "Default";
break;
case "ThemeA":
Profile.MyTheme = "ThemeA";
break;
case "ThemeB":
Profile.MyTheme = "ThemeB";
break;
}
}
Theme = Profile.MyTheme;
}
private void GetCategoryDataBind()
{
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["NewsDBConnectionString"].ConnectionString;
SqlConnection Con = new SqlConnection(constr);
string Sql = "select * from categories";
SqlCommand Cmd = new SqlCommand(Sql, Con);
SqlDataAdapter Sda = new SqlDataAdapter(Cmd);
DataSet Ds = new DataSet();
Sda.Fill(Ds, "Categories");
//Con.Open();
//SqlDataReader Sda = Cmd.ExecuteReader();
this.GridView1.DataSource = Ds.Tables[0].DefaultView;
this.GridView1.DataBind();
//Sda.Close();
//Con.Close();
}
//protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
//{
// this.GridView1.EditIndex = e.NewEditIndex;
// this.GetCategoryDataBind();
//}
//protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
//{
// int Cid = Int32.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
// string constr = System.Configuration.ConfigurationManager.ConnectionStrings["NewsDBConnectionString"].ConnectionString;
// SqlConnection Con = new SqlConnection(constr);
// string Sql = "delete from categories where categoryid=@cid";
// SqlParameter parms = new SqlParameter("@cid", Cid);
// SqlCommand cmd = new SqlCommand(Sql, Con);
// cmd.Parameters.Add(parms);
// Con.Open();
// cmd.ExecuteNonQuery();
// Con.Close();
// this.GetCategoryDataBind();
//}
//protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
//{
// int Cid = Int32.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());
// string catgoryname = ((TextBox)this.GridView1.Rows[e.RowIndex].FindControl("txtcategoryname")).Text;
// string constr = System.Configuration.ConfigurationManager.ConnectionStrings["NewsDBConnectionString"].ConnectionString;
// SqlConnection Con = new SqlConnection(constr);
// // string Sql = "update Categories set CategoryName='" + catgoryname + "' where Categoryid=" + Cid.ToString(); ;
// string Sql = "update categories set categoryname=@cname where categoryid=@cid";
// SqlParameter[] parms = new SqlParameter[2];
// parms[0] = new SqlParameter("@cname", catgoryname);
// parms[1] = new SqlParameter("@cid", Cid);
// SqlCommand cmd = new SqlCommand(Sql, Con);
// cmd.Parameters.AddRange(parms);
// Con.Open();
// cmd.ExecuteNonQuery();
// Con.Close();
// this.GridView1.EditIndex = -1;
// this.GetCategoryDataBind();
//}
//protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
//{
// this.GridView1.EditIndex = -1;
// this.GetCategoryDataBind();
//}
protected void btnAdd_Click(object sender, EventArgs e)
{
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["NewsDBConnectionString"].ConnectionString;
SqlConnection Con = new SqlConnection(constr);
string Sql = "insert into Categories (CategoryName) values(@Cname)";
SqlParameter parms = new SqlParameter("@Cname", this.txtAddcategory.Text.Trim());
SqlCommand Cmd = new SqlCommand(Sql, Con);
Cmd.Parameters.Add(parms);
Con.Open();
Cmd.ExecuteNonQuery();
Con.Close();
this.SqlDataSource2.Insert();
this.GetCategoryDataBind();
this.txtAddcategory.Text = "";
}
//protected void SqlDataSource2_Inserting(object sender, SqlDataSourceCommandEventArgs e)
//{
// e.Command.Parameters["@CategoryName"].Value = this.txtAddcategory.Text.Trim();
//}
protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
//this.GetCategoryDataBind();
GridView1.DataBind();
}
protected void BtnSearch_Click(object sender, EventArgs e)
{
if(txtSearch.Text.Trim()!="")
{
SqlDataSource1.FilterExpression = this.DplistCategory.SelectedValue + " like '%" + this.txtSearch.Text.Trim() + "%'";
}
else
{
SqlDataSource1.FilterExpression = null;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?