您的位置:首页 > 其它

DataList实现编辑,删除,更新,取消,记录转存功能

2012-12-18 20:06 906 查看
前台代码:

<div>

<asp:DataList ID="DataList1" runat="server" oneditcommand="DataList1_EditCommand"

onitemcommand="DataList1_ItemCommand"

oncancelcommand="DataList1_CancelCommand"

ondeletecommand="DataList1_DeleteCommand"

onupdatecommand="DataList1_UpdateCommand">

<EditItemTemplate>

<table style="width:100%;">

<tr>

<td class="style2">

商品名:</td>

<td>

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("ProdctName") %>'></asp:TextBox>

</td>

</tr>

<tr>

<td class="style2">

规格:</td>

<td>

<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("ProdctStandard") %>'></asp:TextBox>

</td>

</tr>

<tr>

<td class="style2">

包装率:</td>

<td>

<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("PackagingRatio") %>'></asp:TextBox>

</td>

</tr>

<tr>

<td class="style2">

商品条码:</td>

<td>

<asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("ArticleNum") %>'></asp:TextBox>

</td>

</tr>

<tr>

<td class="style2">

价格:</td>

<td>

<asp:TextBox ID="TextBox5" runat="server" Text='<%# Eval("Price") %>'></asp:TextBox>

</td>

</tr>

<tr>

<td class="style2">

<asp:Button ID="btnUpdate" runat="server" Text="更新"

CommandArgument='<%# Eval("PId") %>' CommandName="update" />

</td>

<td>

<asp:Button ID="btnCancel" runat="server" Text="取消" CommandName="Cancel" />

</td>

</tr>

</table>

</EditItemTemplate>

<ItemTemplate>

产品名:<asp:Label ID="Label1" runat="server" Text='<%# Eval("ProdctName") %>'></asp:Label>

<br />

规格:<asp:Label ID="Label2" runat="server" Text='<%# Eval("ProdctStandard") %>'></asp:Label>

<br />

包装率:<asp:Label ID="Label3" runat="server" Text='<%# Eval("PackagingRatio") %>'></asp:Label>

<br />

商品条码:<asp:Label ID="Label4" runat="server" Text='<%# Eval("ArticleNum") %>'></asp:Label>

<br />

超市价格:<asp:Label ID="Label5" runat="server" Text='<%# Eval("Price") %>'></asp:Label>

<br />

    

<asp:Button ID="btnedit" runat="server" CommandName="edit" Text="编辑" />

  

<asp:Button ID="btndelete" runat="server" CommandName="delete"

Text="删除" CommandArgument='<%# Eval("PId") %>' />

<br />

  

<asp:Button ID="Button3" runat="server" CommandArgument='<%# Eval("PId") %>'

CommandName="Buy" Height="22px" Text="放入购物车" Width="120px" />

</ItemTemplate>

</asp:DataList>

<br />

</div>

后台代码:

private void DataListBind()

{

string sql = "select * from Product";

DataTable dt = SqlHelper.ExecuteDataTable(sql);

this.DataList1.DataSource = dt;

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;

DataListBind();

}

protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)

{

string name=(e.Item.FindControl("TextBox1") as TextBox).Text.Trim();

string Stand=(e.Item.FindControl("TextBox2") as TextBox).Text.Trim();

string ratio=(e.Item.FindControl("TextBox3") as TextBox).Text.Trim();

string num=(e.Item.FindControl("TextBox4") as TextBox).Text.Trim();

string price=(e.Item.FindControl("TextBox5") as TextBox).Text.Trim();

string sql = "update Product set ProdctName=@name,ProdctStandard=@standard,PackagingRatio=@ratio,ArticleNum=@num,Price=@price
where PId=@id";

SqlParameter[] prm = new SqlParameter[]{

new SqlParameter("name",name), new SqlParameter("standard",Stand), new SqlParameter("ratio",ratio),

new SqlParameter("num",num),new SqlParameter("price",price),new SqlParameter("id",e.CommandArgument)

};

SqlHelper.ExecuteNonQuery(sql,prm);

}

protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)

{

this.DataList1.EditItemIndex = -1;

DataListBind();

}

protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)

{

string sql = "delete from Product where PId=@id";

SqlHelper.ExecuteNonQuery(sql,new SqlParameter("id",e.CommandArgument));

DataListBind();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐