您的位置:首页 > 编程语言 > ASP

asp.net中复选框checkbox选中datalist中的商品后显示选中商品的临时表

2016-09-27 21:17 609 查看
前台

 总价钱:<asp:Label ID="lblsum" runat="server" Text="" CssClass="lab" Visible="false"></asp:Label> 

     <asp:LinkButton ID="lbtDetail"  runat="server" CommandName="Detail" 

            onclick="lbtDetail_Click"><span style="text-decoration:underline;
color:#f74322;
font-size:14px;">点击详情</span></asp:LinkButton> 

     <asp:DataGrid ID="dgList" runat="server" AutoGenerateColumns="true"  AllowPaging="True"

            HorizontalAlign="Center" ShowFooter="True" BackColor="White" 

            BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 

            ForeColor="Black" GridLines="Vertical" Height="79px" Width="270px" Visible="false">

         <FooterStyle BackColor="#CCCCCC" />

         <SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />

         <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />

         <AlternatingItemStyle BackColor="#CCCCCC" />

     <Columns>

     </Columns>

         <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />

     </asp:DataGrid> 

        <asp:Button  ID="btnOK" runat="server" Text="确定" onclick="btnOK_Click" CssClass="btn" Visible="false"/> 

  protected void lbtDetail_Click(object sender,
EventArgs e)

    {

        this.dgList.Visible =
true;//将点餐的datagrid显示

        this.btnOK.Visible =
true;//出现确定的按钮

        this.lblsum.Visible =
true;//将本单总价钱显示出来

        //判断是否选择商品

        try

        {

            //选择商品

            int num = 0;

            //循环判断Datalist

            for (int i = 0; i <
this.list.Items.Count; i++)

            {

                if ((this.list.Items[i].Controls[1]
as CheckBox).Checked ==
true)

                {

                    num = num + 1;

                }

            }

            if (num == 0)

            {

                MessageBox.Show(this,
"请选择商品!");

                return;

            }

            //创建临时表

            DataTable dtFoodSelect =
new DataTable();

            //创建表中列名,注意存入的类型

            dtFoodSelect.Columns.Add("编号",
Type.GetType("System.Int32"));

            dtFoodSelect.Columns.Add("名称",
Type.GetType("System.String"));

            dtFoodSelect.Columns.Add("单价",
Type.GetType("System.String"));

            dtFoodSelect.Columns.Add("份数",
Type.GetType("System.Int32"));

 

            double sum = 0;//此单总价

            //循环遍历datalist

            foreach(DataListItem item
in list.Items)

            {

                   

                    

                    CheckBox ckBox = (CheckBox)item.FindControl("check");//获取datalist中的控件id

                    if(ckBox.Checked==true)

                    {

                        HiddenField hideID = (HiddenField)item.FindControl("giftID");//Datalist隐藏的,用于每个商品的id

                        int id =
int.Parse(hideID.Value);//转化为int类型

                        //获取表中状态是上架的并且是复选框选中的商品

                        DataSet ds =
new DataSetDA().selectData("select * from gift where state=1 and giftID='" + id +
"'");

                        //DataRow是用来表示一个数据表的的数据行

                        DataRow newRow;

                        newRow = dtFoodSelect.NewRow();

                        //给临时表各个列的行赋相应的值

                        newRow["编号"] = ds.Tables[0].Rows[0]["giftID"].ToString();

                        newRow["名称"] = ds.Tables[0].Rows[0]["giftName"].ToString();

                        newRow["单价"] = ds.Tables[0].Rows[0]["giftPrice"].ToString();

                        TextBox tb = (TextBox)item.FindControl("number");//获取datalist中textbox份数的值

                        newRow["份数"] = tb.Text;

                        dtFoodSelect.Rows.Add(newRow);//将这些值加入到临时表中,这句话很关键

                        sum += Convert.ToSingle(ds.Tables[0].Rows[0]["giftPrice"]) *
Convert.ToInt32(tb.Text);//将所有选中的单种商品的单价*份数累加起来

                    }

                }

            dgList.DataSource = dtFoodSelect;//Datagrid的数值来源临时表

            dgList.DataBind();//绑定

            lblsum.Text = sum.ToString()+"元";//datalist全部循环结束,显示此单的总价    

        }

        catch(Exception ex)

        {

            MessageBox.Show(this,
"选择失败!"+ex.Message.ToString());

        }

    }

    protected void btnOK_Click(object sender,
EventArgs e)

    {

        //当此单提交后,临时表和对应的总价消失

        this.dgList.Visible =
false;

        this.btnOK.Visible =
false;

        this.lblsum.Visible =
false;

//也可以根据需要将刚才的消费单写进数据库

    }

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