修改可更新视图中的数据

修改可更新视图中的数据_第1张图片

Default.aspx

View Code
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

"http://www.w3.org/1999/xhtml" >
"Head1" runat="server">
    无标题页

"font-size: 12px">
    
"form1" runat="server">
"border-right: #000099 thin solid; border-top: #000099 thin solid; border-left: #000099 thin solid; width: 400px; border-bottom: #000099 thin solid; height: 1px">
"3" style="text-align: center"> "font-size: 12pt; color: #000099; text-decoration: underline">通过视图修改数据
"3"> "GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Height="138px" Width="440px" style="text-align: center"> "#1C5E55" Font-Bold="True" ForeColor="White" /> "#E3EAEB" /> "#7C6F57" /> "#C5BBAF" Font-Bold="True" ForeColor="#333333" /> "#666666" ForeColor="White" HorizontalAlign="Center" /> "#1C5E55" Font-Bold="True" ForeColor="White" /> "White" />
"width: 119px"> 员工编号:"TextBox1" runat="server" Width="25px"> "width: 312px"> 基本工资:"TextBox2" runat="server" Width="82px"> "Button1" runat="server" OnClick="Button1_Click" Text="修 改" />

Default.aspx.cs

View Code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
        con.Open();
        SqlCommand cmd = new SqlCommand("update View_1 set 基本工资='" + TextBox2.Text + "' where 员工编号='" + TextBox1.Text + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
        this.DataBind();
    }
    private void DataBind()
    {
        SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
        SqlDataAdapter dap = new SqlDataAdapter("select * from View_1", con);
        DataSet ds = new DataSet();
        dap.Fill(ds, "table");
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}

 

转载于:https://www.cnblogs.com/hsw-2013/archive/2013/03/21/2972457.html

你可能感兴趣的:(修改可更新视图中的数据)