using System; using System.Data; using System.Data.SqlClient; using System.Configuration; 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; public partial class _Default : System.Web.UI.Page { private const string connectionStr = @"Data Source=.SQLEXPRESS;AttachDbFilename=D:QianTaoApp_Datadb.mdf;Integrated Security=True;User Instance=True"; protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { SetBind(); } } protected void rptClass_ItemCommand(object source, RepeaterCommandEventArgs e) { //判断是否添加类别 if (e.CommandName == "AddClass") { TextBox tb = e.Item.FindControl("txtClassName") as TextBox; using(SqlConnection conn = new SqlConnection(connectionStr)) { conn.Open(); using(SqlCommand cmd = new SqlCommand("insert into tb_Class(className) values(@className)", conn)) { cmd.Parameters.AddWithValue("@className", tb.Text); cmd.ExecuteNonQuery(); SetBind(); } } } //判断是否删除类别 if (e.CommandName == "DelClass") { using (SqlConnection conn = new SqlConnection(connectionStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("delete from tb_Module where classId=@classId;delete from tb_Class where id=@classId", conn)) { cmd.Parameters.AddWithValue("@classId", e.CommandArgument); cmd.ExecuteNonQuery(); SetBind(); } } } //判断是否修改类别 if (e.CommandName == "UpdateClass") { TextBox tb = e.Item.FindControl("txtClassName") as TextBox; using (SqlConnection conn = new SqlConnection(connectionStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("update tb_Class set className=@className where id=@id", conn)) { cmd.Parameters.AddWithValue("@className", tb.Text); cmd.Parameters.AddWithValue("@id", e.CommandArgument); cmd.ExecuteNonQuery(); SetBind(); } } } //判断是否添加模块 if (e.CommandName == "AddModule") { TextBox tb = e.Item.FindControl("txtModuleName") as TextBox; using (SqlConnection conn = new SqlConnection(connectionStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("insert into tb_Module(classId,moduleName)values(@classId,@moduleName)", conn)) { cmd.Parameters.AddWithValue("@moduleName", tb.Text); cmd.Parameters.AddWithValue("@classId", e.CommandArgument); cmd.ExecuteNonQuery(); SetBind(); } } } } /// <summary> /// 嵌套repeater的ItemCommand事件 /// </summary> protected void rptModule_ItemCommand(object source, RepeaterCommandEventArgs e) { //判断是否删除模块 if (e.CommandName == "DelModule") { using (SqlConnection conn = new SqlConnection(connectionStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("delete from tb_Module where id=@Id;", conn)) { cmd.Parameters.AddWithValue("@Id", e.CommandArgument); cmd.ExecuteNonQuery(); SetBind(); } } } //判断是否修改模块 if (e.CommandName == "UpdateModule") { TextBox tb = e.Item.FindControl("txtModuleName") as TextBox; using (SqlConnection conn = new SqlConnection(connectionStr)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("update tb_Module set moduleName=@Name where id=@id", conn)) { cmd.Parameters.AddWithValue("@Name", tb.Text); cmd.Parameters.AddWithValue("@id", e.CommandArgument); cmd.ExecuteNonQuery(); SetBind(); } } } } /// <summary> /// 绑定Repeater /// </summary> private void SetBind() { DataSet ds = new DataSet(); using(SqlConnection conn = new SqlConnection(connectionStr)) { SqlDataAdapter adapter = new SqlDataAdapter("select * from tb_Class",conn); adapter.Fill(ds); } rptClass.DataSource = ds; rptClass.DataBind(); } protected void rptClass_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Repeater rpt = e.Item.FindControl("rptModule") as Repeater; //找到分类Repeater关联的数据项 DataRowView rowv = (DataRowView)e.Item.DataItem; //提取分类ID int id = (int)rowv["id"]; DataSet ds = new DataSet(); using (SqlConnection conn = new SqlConnection(connectionStr)) { SqlDataAdapter adapter = new SqlDataAdapter("select * from tb_Module where classId="+ id +"", conn); adapter.Fill(ds); } rpt.DataSource = ds; rpt.DataBind(); } } }
ASPX:
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#Eval("nid")+","+this.listpage.CurrentPageIndex.ToString()%>' CommandName="delete">删除</asp:LinkButton> if (e.CommandName == "delete") { string[] cmdArg = e.CommandArgument.ToString().Split(','); string strMsg = "确定要删除吗?"; string strUrl_Yes = "deleteUser.aspx?qid=" + cmdArg[0] + "&page=UserList&pno=" + cmdArg[1]; string strUrl_No = ""; // page + ".aspx?msg=用户取消!&pno=" + Request.QueryString["pno"].ToString(); Response.Write("<Script Language='JavaScript'>if ( window.confirm('" + strMsg + "')) {window.location.href='" + strUrl_Yes + "'} else {};</script>"); return; }