asp.net更新指定记录的方法
本文实例讲述了asp.net更新指定记录的方法。分享给大家供大家参考。具体方法如下:
我们先来看html页面:
<%@PageLanguage="C#"AutoEventWireup="true" CodeFile="Default.aspx.cs"Inherits="_Default"%>
<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执行更新数据库
ViewCode
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程序设计有所帮助。