asp.net更新指定记录的方法
2023-06-13 09:15:30 时间
本文实例讲述了asp.net更新指定记录的方法。分享给大家供大家参考。具体方法如下:
我们先来看html页面:
<formid="form1"runat="server">
<divstyle="text-align:center">
<tablestyle="width:302px;height:246px;">
<tr>
<tdcolspan="2"style="width:496px">
<asp:LabelID="Label2"runat="server"Text="更新指定数据"Font-Bold="True"ForeColor="Blue"Width="132px"></asp:Label></td>
</tr>
<tr>
<tdcolspan="2"style="width:496px">
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"CellPadding="4"Font-Size="Smaller"ForeColor="#333333"GridLines="None">
<Columns>
<asp:BoundFieldDataField="商品编号"HeaderText="商品编号"/>
<asp:BoundFieldDataField="商品名称"HeaderText="商品名称"/>
<asp:BoundFieldDataField="商品数量"HeaderText="商品数量"/>
<asp:BoundFieldDataField="商品单价"HeaderText="商品单价"/>
<asp:HyperLinkFieldDataNavigateUrlFields="商品编号"DataNavigateUrlFormatString="Default.aspx?商品编号={0}"
HeaderText="更新"Text="更新"/>
</Columns>
<FooterStyleBackColor="#5D7B9D"Font-Bold="True"ForeColor="White"/>
<RowStyleBackColor="#F7F6F3"ForeColor="#333333"/>
<EditRowStyleBackColor="#999999"/>
<SelectedRowStyleBackColor="#E2DED6"Font-Bold="True"ForeColor="#333333"/>
<PagerStyleBackColor="#284775"ForeColor="White"HorizontalAlign="Center"/>
<HeaderStyleBackColor="#5D7B9D"Font-Bold="True"ForeColor="White"/>
<AlternatingRowStyleBackColor="White"ForeColor="#284775"/>
</asp:GridView>
</td>
</tr>
<tr>
<tdcolspan="2"style="width:496px"align="center">
</td>
</tr>
<tr>
<tdcolspan="2"style="width:496px">
<asp:LabelID="Label3"runat="server"Font-Size="Smaller"Text="商品名称:"Width="65px"></asp:Label><asp:TextBoxID="TxtName"runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<tdcolspan="2">
<asp:LabelID="Label4"runat="server"Font-Size="Smaller"Text="商品数量:"></asp:Label>
<asp:TextBoxID="TxtNum"runat="server"></asp:TextBox></td>
</tr>
<tr>
<tdcolspan="2">
<asp:LabelID="Label5"runat="server"Font-Size="Smaller"Text="商品单价:"></asp:Label>
<asp:TextBoxID="TxtPrice"runat="server"></asp:TextBox></td>
</tr>
<tr>
<tdcolspan="2"style="width:496px">
<asp:ButtonID="BtnUpdate"runat="server"OnClick="BtnUpdate_Click"Text="更新"Width="55px"/></td>
</tr>
</table>
</div>
</form>
由上面页面提交过来的数据我们接受然后利用sql执行更新数据库
usingSystem;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
usingSystem.Data.SqlClient;
publicpartialclass_Default:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!Page.IsPostBack)//首次执行页面时
{
GridViewBind();//绑定自定义方法GridViewBind
if(Request.QueryString["商品编号"]!=null)//判断,如果可以获取到id的值,则执行以下操作
{
SqlConnectioncon=newSqlConnection(ConfigurationSettings.AppSettings["strCon"]);
con.Open();
SqlDataAdapterada=newSqlDataAdapter("select*fromtb_shopping05where商品编号="+Request.QueryString["商品编号"]+"",con);
DataSetds=newDataSet();
ada.Fill(ds,"tb_shopping05");
DataRowViewdrv=ds.Tables["tb_shopping05"].DefaultView[0];
this.TxtName.Text=drv["商品名称"].ToString();
this.TxtNum.Text=drv["商品数量"].ToString();
this.TxtPrice.Text=drv["商品单价"].ToString();
}
}
}
publicvoidGridViewBind()//绑定GridView控件的自定义方法
{
SqlConnectioncon=newSqlConnection(ConfigurationSettings.AppSettings["strCon"]);
con.Open();
SqlDataAdapterada=newSqlDataAdapter("select*fromtb_shopping05",con);
DataSetds=newDataSet();
ada.Fill(ds);
GridView1.DataSource=ds;
GridView1.DataBind();
con.Close();
}
protectedvoidBtnUpdate_Click(objectsender,EventArgse)
{
try
{
SqlConnectioncon=newSqlConnection(ConfigurationSettings.AppSettings["strCon"]);
con.Open();
SqlCommandcom=newSqlCommand("updatetb_shopping05set商品名称=""+this.TxtName.Text+"",商品数量=""+this.TxtNum.Text+"",商品单价=""+this.TxtPrice.Text+""where商品编号="+Request["商品编号"],con);
com.ExecuteNonQuery();
GridViewBind();
Response.Write("<scriptlanguage=javascript>alert("恭喜您!信息更新成功!")</script>");
}
catch
{
Response.Write("<scriptlanguage=javascript>alert("很遗憾!信息更新失败!")</script>");
}
}
}
原理是这样的,我们点击经编辑的数据时会传一个ID过来,然后我们再利用sql把接受过来的数据进行update即可了。
希望本文所述对大家的asp.net程序设计有所帮助。
相关文章
- ASP登录注册实现MySQL用户管理(asp登录注册mysql)
- ASP编程查询MySQL之道(asp查询mysql)
- ASP操作MySQL简易指南轻松删除记录(asp删除记录mysql)
- ASP.NET页面间的传值的几种方法
- 在asp.net中实现datagridcheckbox全选的方法
- 在ASP.NET中重写URL的代码
- asp.net下利用JS实现对后台CS代码的调用方法
- asp.net(C#)中上传大文件的几中常见应用方法
- asp.net显示页面执行时间
- Asp.Net中的三种分页方式总结
- ASP.NET缓存方法和最佳实践
- asp.net判断数组是否存在某个值的方法
- win2003服务器asp.net权限设置问题及解决方法
- asp.net动态生成rdlc报表(原创)
- ASP.NET动态添加控件一例
- 先装了FRAMEWORK,后装IIS导致asp.net页面无法访问的解决方法
- 浅析ASP.NET安全性分析(加强asp.net1.1/2.0安全性)
- ASP.NET用DataSet导出到Excel的方法
- asp.net中倒计时自动跳转页面的实现方法(使用javascript)
- asp.net得到本机数据库实例的两种方法代码
- asp.net中TextBox只能输入数字的最简洁的两种方法
- Asp.net后台添加CSS、JS、Meta标签的方法
- asp.net后台如何输出js脚本使用什么方法可以实现
- asp.net不同页面间数据传递的多种方法