asp.net更新指定记录的方法
程序员文章站
2024-02-19 20:51:04
本文实例讲述了asp.net更新指定记录的方法。分享给大家供大家参考。具体方法如下:
我们先来看html页面:
复制代码 代码如下:<%@ page langua...
本文实例讲述了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程序设计有所帮助。
上一篇: SQL Server 搜索技巧随记