webform添加修改删除

复制代码
public partial class Edit : 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();
    }

    //在修改页面获取人员主键值加载人员信息
    private void FillLoadInfo()
    {
        string Code = Request["id"];
        var query = _Context.Info.Where(p => p.Code == Code);
        if (query.Count() > 0)
        {
            Info data = query.First();
            txtCode.Text = data.Code;
            txtName.Text = data.Name;
            txtSex.SelectedValue = data.Sex.ToString();
            txtNation.SelectedValue = data.Nation;
            txtBirthday.Text = data.Birthday.Value.ToString("yyyy年MM月dd日");
        }
    }
    //加载民族和个人信息
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillNation();
            FillLoadInfo();
        }
    }

    //返回显示信息的页面
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("修改信息.aspx");
    }

    //修改更新信息
    protected void btnOk_Click(object sender, EventArgs e)
    {
        //查出要修改的对象
        string code = txtCode.Text;
        var query = _Context.Info.Where(p=>p.Code == code);

        if (query.Count() > 0)
        {
            Info data = query.First ();
            //修改
            data.Name  = txtName.Text ;
            data.Sex = Convert.ToBoolean (txtSex.Text);
            data.Nation = txtNation.Text ;
            data.Birthday = Convert.ToDateTime (txtBirthday.Text );

            //提交
            _Context.SubmitChanges();
        }
    }
}
复制代码

Repeater绑定数据:

复制代码
 private MyDBDataContext _Context = new MyDBDataContext();
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Info> list = _Context.Info.ToList();
            Repeater1.DataSource = list;
            Repeater1.DataBind();
        }
    }
复制代码

修改信息超链接:

1.a标签:

<a href="Edit.aspx?id=<%#Eval("Code") %>">修改</a>

2.HyperLink按钮

复制代码
    public string ShowHref()
    {
        return "Edit.aspx?id=" + Eval("Code");
    }
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="<%#ShowHref() %>">修改</asp:HyperLink>
                    </td>







复制代码
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();
    }
复制代码
复制代码
posted @ 2015-08-14 20:42  怎能不累  阅读(289)  评论(0编辑  收藏  举报