WB 例子 修改

前台HTML代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Update.aspx.cs" Inherits="Update" %>

<!DOCTYPE html>

<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>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <div>
        <h1>修改数据</h1>
    </div>
        <asp:Label ID="Label1" runat="server" Text="代号:"></asp:Label>
        <asp:TextBox ID="txtCode" runat="server" Enabled="False"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="姓名:"></asp:Label>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="Label3" runat="server" Text="性别:"></asp:Label>
        <asp:RadioButton ID="rdMan" runat="server" Checked="True" GroupName="sex" Text="男" />
        <asp:RadioButton ID="rdWomen" runat="server" GroupName="sex" Text="女" />
        <br />
        <br />
        <asp:Label ID="Label4" runat="server" Text="民族:"></asp:Label>
        <asp:DropDownList ID="drNation" runat="server">
        </asp:DropDownList>
        <br />
        <br />
        <asp:Label ID="Label5" runat="server" Text="生日:"></asp:Label>
        <asp:TextBox ID="txtBirthday" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="确定" />
  
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="返回" />
    </form>
</body>
</html>

  后台C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Update : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["uid"] != null)
        {
            if (!IsPostBack)
            {
                testDataContext context = new testDataContext();
                //下拉列表填数据
                drNation.DataSource = context.Nation;
                drNation.DataTextField = "Name";
                drNation.DataValueField = "Code";
                drNation.DataBind();

                //取出传过来的code值
                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");
                rdMan.Checked = data.Sex.Value;
                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)
    {
        testDataContext context = new testDataContext();
        //取值
        string code = txtCode.Text;
        string name = txtName.Text;
        bool sex = rdMan.Checked;
        string nation = drNation.SelectedValue;
        DateTime birthday = Convert.ToDateTime(txtBirthday.Text);

        //查到这条数据
        Info data = context.Info.Where(p => p.Code == code).First();
        //修改内容
        data.Name = name;
        data.Sex = sex;
        data.Nation = nation;
        data.Birthday = birthday;

        //提交

        context.SubmitChanges();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Main.aspx");
    }
}

  网页显示图片:

 

posted on 2015-12-21 20:36  第三轮旭  阅读(196)  评论(0编辑  收藏  举报

导航