使用LINQ TO SQL类连接数据库。
create database mydb go use mydb go CREATE TABLE [dbo].[ChinaStates] ( [AreaCode] varchar(20) COLLATE Chinese_PRC_CI_AS NOT NULL, [AreaName] nvarchar(20) COLLATE Chinese_PRC_CI_AS NOT NULL, [ParentAreaCode] varchar(20) COLLATE Chinese_PRC_CI_AS NOT NULL, [Root] bit NOT NULL, [Zone] smallint NOT NULL, [NavigateURL] varchar(100) COLLATE Chinese_PRC_CI_AS, CONSTRAINT [PK_TB_Area] PRIMARY KEY CLUSTERED ([AreaCode]) ) ON [PRIMARY]
编辑页面
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body style="height: 136px"> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="在此级别添加地区" Width="120px" OnClick="Button1_Click" /> <asp:Button ID="Button2" runat="server" Text="返回上级" OnClick="Button2_Click" /> </div> <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"> <HeaderTemplate> <table> <tr> <td>地名</td> <td>编号</td> <td>上一级</td> <td>管理</td> <td>删除</td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <%#Eval("AreaName") %> </td> <td> <%#Eval("AreaCode") %> </td> <td> <%#Eval("ParentAreaCode") %> </td> <td> <asp:LinkButton ID="linkbutton1" CommandArgument='<%#Eval("AreaCode") %>' CommandName="Showxiaji" runat="server">下级地区</asp:LinkButton> </td> <td> <asp:LinkButton ID="linkbutton2" CommandArgument='<%#Eval("AreaCode") %>' OnClientClick='return confirm("确定删除吗?")' CommandName="delete" runat="server">删除</asp:LinkButton> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <asp:Button ID="Button5" runat="server" Text="Button" /> <asp:Literal ID="Literal1" runat="server"></asp:Literal> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:Literal ID="Literal2" runat="server"></asp:Literal> <asp:Panel ID="Panel1" runat="server" Visible="False"> <p> <asp:Label ID="Label1" runat="server" Text="请输入地区:"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </p> <p> <asp:Label ID="Label2" runat="server" Text="请输入邮编:"></asp:Label> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> </p> <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="添加" /> <asp:Button ID="Button4" runat="server" Text="取消" OnClick="Button4_Click" /> </asp:Panel> </form> </body> </html>
编写cs
public DataClassesDataContext context; public List<ChinaStates> chinastates; protected void Page_Load(object sender, EventArgs e) { context = new DataClassesDataContext(); if (!IsPostBack) { binddata("0001"); } } //根据parentcode查 private void binddata(string parentcode) { var querydata = from q in context.ChinaStates where q.ParentAreaCode == parentcode select q; Repeater1.DataSource = querydata; Repeater1.DataBind(); Literal2.Text = parentcode;//显示ParentAreaCode } protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { //删除 if(e.CommandName=="delete") { string code = e.CommandArgument.ToString();//获取这一及编号 ChinaStates cstates = context.ChinaStates.Single(r=>r.AreaCode==code);//获取对应地区编号的数据 context.ChinaStates.DeleteOnSubmit(cstates);//删除 context.SubmitChanges(); binddata(cstates.ParentAreaCode);//显示删除后的数据 } //显示下级 if(e.CommandName=="Showxiaji") { string code = e.CommandArgument.ToString();//获取这一级的唯一AreaCode ChinaStates cstates = context.ChinaStates.Single(r => r.AreaCode == code); Literal1.Text = cstates.ParentAreaCode;//存储这级的ParentAreaCode binddata(code);//显示,下一级ParentCode是这级的AreaCode } } //添加地区 protected void Button3_Click(object sender, EventArgs e) { ChinaStates cdata = new ChinaStates() { AreaCode=DateTime.Now.Millisecond.ToString(), AreaName=TextBox1.Text, ParentAreaCode=Literal2.Text }; try { context.ChinaStates.InsertOnSubmit(cdata); context.SubmitChanges(); binddata(Literal2.Text); Response.Write("<script>alter('添加成功!')</script>"); } catch(Exception ex) { Response.Write("<script>alter('添加失败!')</script>"); } } protected void Button1_Click(object sender, EventArgs e) { Panel1.Visible = true; } //返回上级 protected void Button2_Click(object sender, EventArgs e) { if (Literal1.Text != "0000" && Literal1.Text.Length > 0) { binddata(Literal1.Text);//显示上一级,ParentAreaCode ChinaStates cdata = context.ChinaStates.Single(r=>r.AreaCode==Literal1.Text); Literal1.Text = cdata.ParentAreaCode; } } }