使用DataList实现 加入购物车,编辑,删除,更新,取消功能。要求连一个产品表。
<body>
<form id="form1" runat="server">
<p>
<:datalist id="datalist1" runat="server"
onitemcommand="datalist1_itemcommand"
oncancelcommand="datalist1_cancelcommand"
ondeletecommand="datalist1_deletecommand" oneditcommand="datalist1_editcommand"
onupdatecommand="datalist1_updatecommand">
<edititemtemplate> www.2cto.com
<table style="width: 100%; height: 180px;">
<tr>
<td class="style4">
商品名:</td>
<td class="style2">
<asp:textbox id="txtproductname" runat="server"
text='<%# eval("productname") %>'></asp:textbox>
</td>
</tr>
<tr>
<td class="style4">
规格:</td>
<td class="style2">
<asp:textbox id="txtproductstandard" runat="server"
text='<%# eval("productstandard") %>'></asp:textbox>
</td>
</tr>
<tr>
<td class="style4">
包装率:</td>
<td class="style2">
<asp:textbox id="txtpackagingratio" runat="server"
text='<%# eval("packagingratio") %>'></asp:textbox>
</td>
</tr>
<tr>
<td class="style4">
商品条码:</td>
<td class="style2">
<asp:textbox id="txtarticlenum" runat="server" text='<%# eval("articlenum") %>'></asp:textbox>
</td>
</tr>
<tr>
<td class="style4">
价格:</td>
<td class="style2">
<asp:textbox id="txtprice" runat="server" text='<%# eval("price") %>'></asp:textbox>
</td>
</tr>
<tr>
<td class="style4">
<asp:button id="btnupdate" runat="server" commandargument='<%# eval("pid") %>'
commandname="update" height="21px" text="更新" />
</td>
<td class="style2">
<asp:button id="btncancel" runat="server" commandname="cancel" text="取消" />
</td>
</tr>
</table>
</edititemtemplate>
<itemtemplate>
产品名:<asp:label id="lblproductname" runat="server"
text='<%# eval("productname") %>'></asp:label>
<br />
规格:<asp:label id="lblproductstandard" runat="server"
text='<%# eval("productstandard") %>'></asp:label>
<br />
包装率:<asp:label id="lblpackagingratio" runat="server"
text='<%# eval("packagingratio") %>'></asp:label>
<br />
商品条码:<asp:label id="lblarticlenum" runat="server"
text='<%# eval("articlenum") %>'></asp:label>
<br />
超市价格:<asp:label id="lblprice" runat="server" text='<%# eval("price") %>'></asp:label>
<br />
<asp:button id="btnedit" runat="server" text="编辑" commandname="edit" />
<asp:button id="btndelete" runat="server" text="删除"
commandargument='<%# eval("pid") %>' commandname="delete" />
<br />
<br />
<asp:button id="btngouwuche" runat="server" commandargument='<%# eval("pid") %>'
commandname="buy" text="放入购物车" />
<br />
</itemtemplate>
</asp:datalist>
<br />
</p>
</form>
</body>
----------------后台----------------------
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
bindproduct();
}
}
private void bindproduct()
{
string sql = "select * from product";
datatable dt = sqlhelper.executedatatable(sql);
this.datalist1.datasource = dt;
this.datalist1.databind();
}
protected void datalist1_itemcommand(object source, datalistcommandeventargs e)
{
if (e.commandname == "buy")
{
string proname = (e.item.findcontrol("lblproductname") as label).text;
string prostandarde = (e.item.findcontrol("lblproductstandard") as label).text;
string propackaging = (e.item.findcontrol("lblpackagingratio") as label).text;
string proartialenum = (e.item.findcontrol("lblarticlenum") as label).text;
string proprice = (e.item.findcontrol("lblprice") as label).text;
string sql = "insert into shoppingcart(productname,productstandard,packagingratio,articlenum,price) values(@productname,@productstandard,@packagingratio,@articlenum,@price)";
sqlparameter[] pms = new sqlparameter[] {
new sqlparameter("@productname",proname),
new sqlparameter("@productstandard",prostandarde),
new sqlparameter("@packagingratio",propackaging),
new sqlparameter("@articlenum",proartialenum),
new sqlparameter("@price",proprice)
};
sqlhelper.executenonquery(sql, pms);
}
}
protected void datalist1_editcommand(object source, datalistcommandeventargs e)
{
this.datalist1.edititemindex = e.item.itemindex;
this.bindproduct();
}
protected void datalist1_updatecommand(object source, datalistcommandeventargs e)
{
string proname=(e.item.findcontrol("txtproductname") as textbox).text;
string prostandarde=(e.item.findcontrol("txtproductstandard") as textbox).text;
string propackaging=(e.item.findcontrol("txtpackagingratio") as textbox).text;
string proartialenum=(e.item.findcontrol("txtarticlenum") as textbox).text;
string proprice = (e.item.findcontrol("txtprice") as textbox).text;
string sql = "update product set productname=@productname,productstandard=@productstandard,packagingratio=@packagingratio,articlenum=@articlenum,price=@price where pid=@pid";
sqlparameter[] pms = new sqlparameter[]{
new sqlparameter("@productname",proname),
new sqlparameter("@productstandard",prostandarde),
new sqlparameter("@packagingratio",propackaging),
new sqlparameter("@articlenum",proartialenum),
new sqlparameter("@price",proprice),
new sqlparameter("@pid",e.commandargument)
};
sqlhelper.executenonquery(sql, pms);
}
protected void datalist1_cancelcommand(object source, datalistcommandeventargs e)
{
this.datalist1.edititemindex = -1;
this.bindproduct();
}
protected void datalist1_deletecommand(object source, datalistcommandeventargs e)
{
string sql = "delete from product where pid=@pid";
sqlparameter pms = new sqlparameter("@pid", e.commandargument);
sqlhelper.executenonquery(sql, pms);
this.bindproduct();
}
------------------------web.config:-----------------------------------
<connectionstrings>
<add name="studentconnectionstring" connectionstring="data source=pc_think-think;initial catalog=student;persist security info=true;user id=sa;password=111111"
providername="system.data.sqlclient" />
</connectionstrings>
----------------------sqlhelper类:-------------------------------------
public static string connstr = configurationmanager.connectionstrings["studentconnectionstring"].connectionstring;
public static int executenonquery(string sql, params sqlparameter[] pms)
{
using (sqlconnection con = new sqlconnection(connstr))
{
using (sqlcommand cmd = new sqlcommand(sql, con))
{
if (pms != null)
{
cmd.parameters.addrange(pms);
}
con.open();
return cmd.executenonquery();
}
}
}
public static datatable executedatatable(string sql, params sqlparameter[] pms)
{
datatable dt = new datatable();
sqldataadapter adapter = new sqldataadapter(sql,connstr);
if (pms != null)
{
adapter.selectcommand.parameters.addrange(pms);
}
adapter.fill(dt);
return dt;
}
上一篇: 百度分词技术和算法分析