zoukankan      html  css  js  c++  java
  • Webform---添加删除人员信息

    public partial class Add : System.Web.UI.Page
    {
        private MyDBDataContext _Context = new MyDBDataContext();
    
        //绑定民族
        private void FillNation()
        {
            List<Nation> list = _Context.Nation.ToList();
            txtNation.DataSource = list;
            txtNation.DataTextField = "Name";
            txtNation.DataValueField = "Code";
            txtNation.DataBind();
    
        }
    
        //加载民族
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillNation();
            }
        }
    
        //点击添加按钮
        protected void btnOk_Click(object sender, EventArgs e)
        {
            //把界面的值取出来
            string code = txtCode.Text;
            string name = txtName.Text;
            bool sex = Convert.ToBoolean( txtSex.SelectedValue);
            string nation = txtNation.Text;
            DateTime birthday = Convert.ToDateTime( txtBirthday.Text);
    
            //送到数据库去
            Info data = new Info();
            data.Code = code;
            data.Name = name;
            data.Sex = sex;
            data.Nation = nation;
            data.Birthday = birthday;
            _Context.Info.InsertOnSubmit(data);
            _Context.SubmitChanges();
    
            //跳转
            Response.Redirect("修改信息.aspx");
        }
    
        //点击返回按钮
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("修改信息.aspx");
        }
    }

    删除:

    1.使用a标签和HyperLink按钮

     public string SetKey()
        {
            return "Delete.aspx?id=" + Eval("Code");
        }

    //a 标签
    <a href ="Delete.aspx?id=<%#Eval("Code") %>" onclick="return confirm('确认删除?')">删除</a>

    //HyperLink按钮 <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="<%#SetKey() %>">删除</asp:HyperLink>
    public partial class Delete : System.Web.UI.Page { private MyDBDataContext _Context = new MyDBDataContext(); protected void Page_Load(object sender, EventArgs e) { //找出主键值 string code = Request["id"]; //删除 //找到要删除的项 var query = _Context.Info.Where(p=> p.Code == code); if (query.Count() > 0) { Info data = query.First(); _Context.Info.DeleteOnSubmit(data); _Context.Work.DeleteAllOnSubmit(data.Work); _Context.Family.DeleteAllOnSubmit(data.Family); //提交 _Context.SubmitChanges(); } //跳转 Response.Redirect("修改信息.aspx"); } }

    2.使用普通按钮

        public string Delete()
        {
            return Eval("Code").ToString ();
        }
    
    //普通按钮
    <asp:Button ID="Button1" OnClick="Button1_Click" CommandArgument="<%#Delete() %>" runat="server" Text="删除" />
    
        private void Show()
        {
            List<Info> list = _Context.Info.ToList();
            Repeater1.DataSource = list;
            Repeater1.DataBind();
        }
    
        //使用普通按钮删除信息
        protected void Button1_Click(object sender, EventArgs e)
        {
            //找到被点击的删除按钮
            Button btn = (Button)sender;
    
            //找到按钮上的主键值
            string code = btn.CommandArgument.ToString();
    
            //找到要删除的项
            var query = _Context.Info.Where(p => p.Code == code);
            if (query.Count() > 0)
            {
                Info data = query.First();
                _Context.Info.DeleteOnSubmit(data);
                _Context.Work.DeleteAllOnSubmit(data.Work);
                _Context.Family.DeleteAllOnSubmit(data.Family);
    
                //提交
                _Context.SubmitChanges();
            }
    
            Show();
        }
  • 相关阅读:
    福大软工 · 第十次作业
    最终作业:软工实践个人总结
    第三视角Beta答辩总结
    Beta 冲刺(7/7)
    Beta 冲刺 (6/7)
    Beta 冲刺 (5/7)
    Beta 冲刺 (4/7)
    Beta 冲刺 (3/7)
    Beta 冲刺 (2/7)
    福大软工 · 第十次作业
  • 原文地址:https://www.cnblogs.com/qianxiaojinnian/p/4721468.html
Copyright © 2011-2022 走看看