本文实例讲述了asp.net更新指定记录的方法。分享给大家供大家参考。具体方法如下:
我们先来看html页面:
代码如下:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<form id="form1" runat="server">
<div style="text-align: center">
<table style="width: 302px; height: 246px;">
<tr>
<td colspan="2" style="width: 496px">
<asp:Label ID="Label2" runat="server" Text="更新指定数据" Font-Bold="True" ForeColor="Blue" Width="132px"></asp:Label></td>
</tr>
<tr>
<td colspan="2" style="width: 496px">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" Font-Size="Smaller" ForeColor="#333333" GridLines="None">
<Columns>
<asp:BoundField DataField="商品编号" HeaderText="商品编号" />
<asp:BoundField DataField="商品名称" HeaderText="商品名称" />
<asp:BoundField DataField="商品数量" HeaderText="商品数量" />
<asp:BoundField DataField="商品单价" HeaderText="商品单价" />
<asp:HyperLinkField DataNavigateUrlFields="商品编号" DataNavigateUrlFormatString="Default.aspx?商品编号={0}"
HeaderText="更新" Text="更新" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</td>
</tr>
<tr>
<td colspan="2" style="width: 496px" align="center">
</td>
</tr>
<tr>
<td colspan="2" style="width: 496px">
<asp:Label ID="Label3" runat="server" Font-Size="Smaller" Text="商品名称:" Width="65px"></asp:Label><asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="Label4" runat="server" Font-Size="Smaller" Text="商品数量:"></asp:Label>
<asp:TextBox ID="TxtNum" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="Label5" runat="server" Font-Size="Smaller" Text="商品单价:"></asp:Label>
<asp:TextBox ID="TxtPrice" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2" style="width: 496px">
<asp:Button ID="BtnUpdate" runat="server" OnClick="BtnUpdate_Click" Text="更新" Width="55px" /></td>
</tr>
</table>
</div>
</form>
由上面页面提交过来的数据我们接受然后利用sql执行更新数据库
代码如下: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)
{
if (!Page.IsPostBack)//首次执行页面时
{
GridViewBind();//绑定自定义方法GridViewBind
if (Request.QueryString["商品编号"] != null)//判断,如果可以获取到id的值,则执行以下操作
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
con.Open();
SqlDataAdapter ada = new SqlDataAdapter("select * from tb_shopping05 where 商品编号=" + Request.QueryString["商品编号"] + "", con);
DataSet ds = new DataSet();
ada.Fill(ds, "tb_shopping05");
DataRowView drv = ds.Tables["tb_shopping05"].DefaultView[0];
this.TxtName.Text = drv["商品名称"].ToString();
this.TxtNum.Text = drv["商品数量"].ToString();
this.TxtPrice.Text = drv["商品单价"].ToString();
}
}
}
public void GridViewBind()//绑定GridView控件的自定义方法
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
con.Open();
SqlDataAdapter ada = new SqlDataAdapter("select * from tb_shopping05", con);
DataSet ds = new DataSet();
ada.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void BtnUpdate_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
con.Open();
SqlCommand com = new SqlCommand("update tb_shopping05 set 商品名称='" + this.TxtName.Text + "',商品数量='" + this.TxtNum.Text + "',商品单价='" + this.TxtPrice.Text + "' where 商品编号=" + Request["商品编号"], con);
com.ExecuteNonQuery();
GridViewBind();
Response.Write("<script language=javascript>alert('恭喜您!信息更新成功!')</script>");
}
catch
{
Response.Write("<script language=javascript>alert('很遗憾!信息更新失败!')</script>");
}
}
}
原理是这样的,我们点击经编辑的数据时会传一个ID过来,然后我们再利用sql把接受过来的数据进行update即可了。
希望本文所述对大家的asp.net程序设计有所帮助。
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com