1、博客制作代码博客制作代码using System;using System.Collections;using System.Collections.Generic;using System.Configuration;using System.Data;/添加DateSet类所在的命名空间using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using Sy
2、stem.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;/添加数据库访问类的命名空间public partial class BlogManager_BlogType : System.Web.UI.Page private static int blogid = -1; protected void Page_Load(object sender, EventArgs e) if (null = Sessionname) Response.Redirect(/login.aspx);
3、 if (!IsPostBack)/页面首次加载 blogid = GetBlogId(); GVBind(); / / 从数据库中读取类型信息,显示在GridView上 / private void GVBind() /读取数据 String strsql = select * from ST_class where BlogId=id; SqlParameter parms = new SqlParameter (id,System .Data .SqlDbType .Int ), ; parms0.Value = blogid; DataSet ds = SqlHelper.Exectu
4、eDataSet(strsql, parms); /和GridView控件绑定,显示 gv_datalist.DataSource = ds.Tables0; gv_datalist.DataBind(); / / 通过会员名,来获得会员编号 / / private int GetBlogId() String sqlstr = select BlogId from tb_Blog where Username=name; /定义参数,并指定参数类型 SqlParameter parms= new SqlParameter (name,System .Data .SqlDbType .NVar
5、Char,50), ; /给参数赋值 parms0.Value = Sessionname.ToString(); SqlConnection conn = SqlHelper.GetConnection(); SqlDataReader dr = SqlHelper.ExectueReader(sqlstr, parms, conn); int id = -1; if (dr.Read() id = Convert.ToInt32(dr0); dr.Close(); conn.Close(); return id; protected void btn_Add_Click(object se
6、nder, EventArgs e) /读入文章类型名称 String typename = txt_type.Text; /判断该类型是否已经存在 String sqlstr = select count(*) from ST_class where ST_c_name=typename and BlogId=blogid; /定义参数,并指定参数的类型 SqlParameter parms = new SqlParameter (typename,System.Data.SqlDbType.NVarChar,50), new SqlParameter (blogid,System.Data
7、.SqlDbType.Int), ; /指定参数的值 parms0.Value = typename; parms1.Value = blogid; int k = Convert.ToInt32(SqlHelper.ExecuteScalar(sqlstr, parms); if (k 0) /存在,则提示该类型已存在 Response.Write(alert(该类型已存在!); else /不存在,则添加 sqlstr = insert into ST_class values(name,id); SqlParameter parms2 = new SqlParameter (name,S
8、ystem .Data .SqlDbType .NVarChar ,50), new SqlParameter (id,System .Data .SqlDbType .Int ), ; parms20.Value = typename; parms21.Value = blogid; SqlHelper.Excute(sqlstr, parms2); Response.Write(alert(添加成功!); GVBind(); / / GridView进入编辑状态 / / / protected void gv_datalist_RowEditing(object sender, GridV
9、iewEditEventArgs e) gv_datalist.EditIndex = e.NewEditIndex; GVBind(); / / GridView的取消 / / / protected void gv_datalist_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) gv_datalist.EditIndex = -1; GVBind(); / / GridView的修改 / / / protected void gv_datalist_RowUpdating(object sender, Grid
10、ViewUpdateEventArgs e) /读入修改后的数据 long id = Convert.ToInt64(gv_datalist.DataKeyse.RowIndex.Value);/要修改的文章类型的编号 String typename = (TextBox)(gv_datalist.Rowse.RowIndex.Cells2.Controls0).Text;/要修改的文章类型名称 /将修改的内容更新到数据库中 String sqlstr = update ST_class set ST_c_name=typename where ST_c_id=id; SqlParameter
11、 parms = new SqlParameter (typename,System.Data .SqlDbType .NVarChar ,50), new SqlParameter (id,System .Data .SqlDbType .BigInt ), ; parms0.Value = typename; parms1.Value = id; SqlHelper.Excute(sqlstr, parms); Response.Write(alert(修改类型名称成功!); gv_datalist.EditIndex = -1; GVBind(); / / GridView删除 / /
12、/ protected void gv_datalist_RowDeleting(object sender, GridViewDeleteEventArgs e) /获得要删除的记录的文章类型编号 long id = Convert.ToInt64(gv_datalist.DataKeyse.RowIndex.Value);/类型的编号 /删除 String strsql = delete from ST_class where ST_c_id=id; SqlParameter parms = new SqlParameter (id,System .Data .SqlDbType .Big
13、Int ), ; parms0.Value = id; SqlHelper.Excute(strsql, parms); Response.Write(alert(删除成功!); GVBind(); / / 模糊查找 / / / protected void btn_search_Click(object sender, EventArgs e) /判断输入框中有无输入 if (txt_key.Text.Trim().Equals() /无,则显示所有内容 GVBind(); return; else /有,则按关键字进行查询 String search = ddl_select.Select
14、edValue;/查询条件 String key = txt_key.Text;/查找关键字 String sqlstr; SqlParameter parms; /按照文章类型编号进行模糊查询 if (search.Equals(文章编号) sqlstr = select * from ST_class where ST_c_id like % + id + %; parms = new SqlParameter1; parms0 = new SqlParameter(id, System.Data.SqlDbType.NVarChar); parms0.Value = key; /按照文章
15、类型名进行模糊查询 else sqlstr = select * from ST_class where ST_c_name like % +name+ %and BlogID=id; parms = new SqlParameter2; parms0 = new SqlParameter(name, System.Data.SqlDbType.NVarChar, 50); parms1 = new SqlParameter(id, System.Data.SqlDbType.Int); parms0.Value = key; parms1.Value = blogid; DataSet ds
16、 = SqlHelper.ExectueDataSet(sqlstr, parms); /和GridView进行绑定 gv_datalist.DataSource = ds.Tables0; gv_datalist.DataBind(); / / 全选 / / / protected void cb_All_CheckedChanged(object sender, EventArgs e) /如果全选是选中的 if (cb_All.Checked) /则将GridView所有行的复选框都变为选中状态 for (int i = 0; i gv_datalist.Rows.Count; i+)
17、(CheckBox)(gv_datalist.Rowsi.Cells0.FindControl(CheckBox1).Checked = true; / / 取消全选 / / / protected void btn_cancelAll_Click(object sender, EventArgs e) /则将GridView所有行的复选框都变为未选中状态 for (int i = 0; i gv_datalist.Rows.Count; i+) (CheckBox)(gv_datalist.Rowsi.Cells0.FindControl(CheckBox1).Checked =false
18、; cb_All.Checked = false; / / 批量删除 / / / protected void btn_delete_Click(object sender, EventArgs e) /找到要删除的所有记录 /构建要用于执行删除的sql语句 String sql = delete from ST_class where ST_c_id in (; for (int i = 0; i 0) Response.Write(alert(删除成功,共删除 + j .ToString()+ 条记录!); else Response.Write(alert(删除失败!); GVBind();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1