asp.net更新指定记录的方法
更新时间:2021年9月22日 10:11 点击:1417
本文实例讲述了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>
<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>");
}
}
}
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程序设计有所帮助。
相关文章
- 这篇文章主要为大家详细介绍了ASP.NET购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
- 在开发过程中,使用Visual Studio的断点调试功能可以很方便帮我们调试发现程序存在的错误,同样Visual Studio也支持对SQL Server里面的存储过程进行调试,下面就让我们看看具体的调试方法。...2021-09-22
ASP.NET Core根据环境变量支持多个 appsettings.json配置文件
这篇文章主要介绍了ASP.NET Core根据环境变量支持多个 appsettings.json配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22- 这篇文章主要介绍了记一次EFCore类型转换错误及解决方案,帮助大家更好的理解和学习使用asp.net core,感兴趣的朋友可以了解下...2021-09-22
详解ASP.NET Core 中基于工厂的中间件激活的实现方法
这篇文章主要介绍了ASP.NET Core 中基于工厂的中间件激活的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-22Mybatis Plus 字段为空值时执行更新方法未更新解决方案
这篇文章主要介绍了Mybatis Plus 字段为空值时执行更新方法未更新解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-03vivo OriginOS新系统如何更新 originos系统更新方法
vivo新系统更新的步骤是什么?如何更新到vivo的最新系统?vivo的最新系统太亮眼了,不少vivo的用户都在跃跃欲试想体验一下最新的系统。vivo新系统虽然做出来了不过我们想体验的话还是要等待一段时间。到时大家通过下面的方法就可以使用到新系统了...2020-12-08asp.net通过消息队列处理高并发请求(以抢小米手机为例)
这篇文章主要介绍了asp.net通过消息队列处理高并发请求(以抢小米手机为例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22ASP.NET单选按钮控件RadioButton常用属性和方法介绍
RadioButton又称单选按钮,其在工具箱中的图标为 ,单选按钮通常成组出现,用于提供两个或多个互斥选项,即在一组单选钮中只能选择一个...2021-09-22ASP.NET 2.0中的数据操作:使用两个DropDownList过滤的主/从报表
在前面的指南中我们研究了如何显示一个简单的主/从报表, 该报表使用DropDownList和GridView控件, DropDownList填充类别,GridView显示选定类别的产品. 这类报表用于显示具有...2016-05-19ASP.NET中iframe框架点击左边页面链接 右边显示链接页面内容
这篇文章主要介绍了ASP.NET中iframe框架点击左边页面链接,右边显示链接页面内容的实现代码,感兴趣的小伙伴们可以参考一下...2021-09-22- ASP.NET Web API具有与ASP.NET MVC类似的编程方式,ASP.NET Web API不仅仅具有一个完全独立的消息处理管道,而且这个管道比为ASP.NET MVC设计的管道更为复杂,功能也更为强大。下面创建一个简单的Web API项目,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了Android studio4.1更新后出现的问题详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-14
- 今天小编就为大家分享一篇pytorch 自定义参数不更新方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-29
- 这篇文章主要介绍了ASP.NET连接MySql数据库的2个方法及示例,使用的是MySQL官方组件和ODBC.NET,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了Asp.Net使用Bulk实现批量插入数据的方法,对于进行asp.net数据库程序设计非常有借鉴价值,需要的朋友可以参考下...2021-09-22
在ASP.NET 2.0中操作数据之二十九:用DataList和Repeater来显示数据
本文主要讲解ASP.NET 2.0中如何使用DataList 和 Repeater 来呈现数据,DataList包含一个table标记,而Repeater不会添加任何额外的代码,个人在实际开发中更推荐使用Repeater。...2021-09-22- 这篇文章主要介绍了获取DataTable选择第一行某一列值,需要的朋友可以参考下...2021-09-22
- winform程序相对web程序而言,功能更强大,编程更方便,但软件更新却相当麻烦,要到客户端一台一台地升级,面对这个实际问题,在最近的一个小项目中,本人设计了一个通过软件实现自动升级技术方案,弥补了这一缺陷,有较好的参考价值...2020-06-25
- 这篇文章主要介绍了ASP.Net中的async+await异步编程的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22