20151221:Web复习:修改

后台:

public partial class Update : System.Web.UI.Page
{
    private TextDataContext context = new TextDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["uid"] != null)
        {
            if (!IsPostBack)
            {
                //给民族下拉列表绑定数据
                drNation.DataSource = context.Nation;
                drNation.DataTextField = "Name";
                drNation.DataValueField = "Code";
                drNation.DataBind();
                //取主键值
                string code = Request["code"].ToString();
                //查数据库
                Info data = context.Info.Where(p => p.Code == code).First();
                //填上数据
                txtCode.Text = data.Code;
                txtName.Text = data.Name;
                txtBirthday.Text = data.Birthday.Value.ToString("yyyy年MM月dd日");
                if (data.Sex == true)
                {
                    rdnan.Checked = true;
                }
                else
                {
                    rdnv.Checked = true;
                }
                foreach( ListItem item in drNation.Items )
                {
                    if(item.Value == data.Nation)
                    {
                        item.Selected = true;
                    }
                }
            }
        }
        else
        {
            Response.Redirect("Denglu.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //查到这条数据
        Info data = context.Info.Where(p => p.Code == txtCode.Text).First();
        //修改内容
        data.Name = txtName.Text;
        data.Sex = rdnan.Checked;
        data.Nation = drNation.SelectedValue;
        data.Birthday = Convert.ToDateTime(txtBirthday.Text);
        //提交
        context.SubmitChanges();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Main.aspx");
    }

posted @ 2015-12-24 22:40  m-n  阅读(146)  评论(0编辑  收藏  举报