Use DataList to add, edit, delete, update, and cancel a shopping cart. A product table is required.

Source: Internet
Author: User

<Body> <form id = "form1" runat = "server"> <div> <asp: dataList ID = "DataList1" runat = "server" onitemcommand = "DataList1_ItemCommand" oncancelcommand = "inline" ondeletecommand = "inline" oneditcommand = "inline" onupdatecommand = "inline"> <EditItemTemplate> www.2cto.com <table style = "width: 100%; height: 180px; "> <tr> <td class =" style4 "> product name: </t D> <td class = "style2"> <asp: TextBox ID = "txtProductName" runat = "server" Text = '<% # Eval ("ProductName ") %> '> </asp: TextBox> </td> </tr> <td class = "style4"> type: </td> <td class = "style2"> <asp: TextBox ID = "txtProductStandard" runat = "server" Text = '<% # Eval ("ProductStandard ") %> '> </asp: TextBox> </td> </tr> <td class = "style4"> packaging rate: </td> <td class = "style2"> <asp: TextBox ID = "txtPackagingRatio" runat = "Server" Text = '<% # Eval ("PackagingRatio") %>'> </asp: textBox> </td> </tr> <td class = "style4"> product barcode: </td> <td class = "style2"> <asp: textBox ID = "txtArticleNum" runat = "server" Text = '<% # Eval ("ArticleNum") %>'> </asp: textBox> </td> </tr> <td class = "style4"> price: </td> <td class = "style2"> <asp: textBox ID = "txtPrice" runat = "server" Text = '<% # Eval ("Price") %>'> </asp: textBox> </td> </tr> <td clas S = "style4"> <asp: Button ID = "btnUpdate" runat = "server" CommandArgument = '<% # Eval ("PId ") %> 'commandname = "update" Height = "21px" Text = "update"/> </td> <td class = "style2"> <asp: button ID = "btnCancel" runat = "server" CommandName = "cancel" Text = "cancel"/> </td> </tr> </table> </EditItemTemplate> <itemTemplate> product name: <asp: Label ID = "lblProductName" runat = "server" Text = '<% # Eval ("ProductName") %>'> </asp: Label> <br/> Specifications: <asp: Label ID = "lblProductStandard" runat = "server" Text = '<% # Eval ("ProductStandard") %>'> </asp: label> <br/> packaging rate: <asp: Label ID = "lblPackagingRatio" runat = "server" Text = '<% # Eval ("PackagingRatio ") %> '> </asp: Label> <br/> item bar code: <asp: label ID = "lblArticleNum" runat = "server" Text = '<% # Eval ("ArticleNum") %>'> </asp: Label> <br/> Supermarket Price: <asp: Label ID = "lblPrice" runat = "server" Text = '<% # Eval ("Price") %>'> </ Asp: Label> <br/> <asp: Button ID = "btnEdit" runat = "server" Text = "Edit" CommandName = "Edit"/> & nbsp; <asp: button ID = "btnDelete" runat = "server" Text = "delete" CommandArgument = '<% # Eval ("PId ") %> 'commandname = "delete"/> <br/> <asp: button ID = "btnGouWuChe" runat = "server" CommandArgument = '<% # Eval ("PId ") %> 'commandname = "Buy" Text = ""/> <br/> </ItemTemplate> </asp: dataList> <br/> </div> </form> </ Body> ---------------- background ---------------------- 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 Pr OStandarde = (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 DataLi St1_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. findC Ontrol ("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 SqlParamet Er [] {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: ----------------------------------- <connectionSt Rings> <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 class: ----------------------------------------- public static String connStr = ConfigurationManager. connectionStrings ["studentConnectionString"]. conne CtionString; 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.