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>

 

posted on 2015-08-11 11:48  浅笑瑾年  阅读(291)  评论(0编辑  收藏  举报