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;
//也可以根据需要将刚才的消费单写进数据库
}
总价钱:<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;
//也可以根据需要将刚才的消费单写进数据库
}
相关文章推荐
- asp.net CheckBoxList 取值与勾选,复选框后台控制前台checkbox选中
- ASP.NET jQuery 食谱5 (显示CheckBoxList成员选中的内容)
- ASP.NET jQuery 实例5 (显示CheckBoxList成员选中的内容)
- ASP.NET jQuery 实例5 (显示CheckBoxList成员选中的内容)
- asp.net 获得有复选框形式的TreeView的选中的节点值,获得树里复选框checkbox的值
- asp.net CheckBoxList 取值与勾选,复选框后台控制前台checkbox选中
- asp.net CheckBoxList 取值与勾选,复选框后台控制前台checkbox选中
- ASP.NET jQuery 随笔 显示CheckBoxList成员选中的内容
- asp.net CheckBoxList 取值与勾选,复选框后台控制前台checkbox选中
- [ASP.NET]PagedDataSource实现DataList和Repeater分页显示
- Scott Mitchell 的ASP.NET 2.0数据教程之三十一::使用DataList来一行显示多条记录
- asp.net Repeater取得CheckBox选中的某行某个值
- ASP.NET DEMO 10: 如何通过 javascript 访问 GridView/DataGrid 选中 CheckBox 行各列的值
- ASP.NET 2.0 利用 checkbox获得选中行的行号, 在footer中显示
- Scott Mitchell 的ASP.NET 2.0数据教程之二十九:: 用DataList和Repeater来显示数据
- Scott Mitchell 的ASP.NET 2.0数据教程之二十九:: 用DataList和Repeater来显示数据
- asp.net Repeater取得CheckBox选中的某行某个值
- asp.net Repeater取得CheckBox选中的某行某个值的c#写法
- Scott Mitchell 的ASP.NET 2.0数据教程之二十九:: 用DataList和Repeater来显示数据
- ASP.NET DEMO Ⅱ: 使用嵌套DataList显示主从关系