本文介绍了将问题从会话表加载到GridView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
选择产品后,我无法从Session中加载表格.
Product.aspx:
I can''t load table from Session after selecting the product.
Product.aspx:
<asp:DataList ID="DataList1" runat="server" Height="176px"
HorizontalAlign="Center" RepeatColumns="5" RepeatDirection="Horizontal"
Width="16px" onitemcommand="DataList1_ItemCommand">
<ItemTemplate>
<asp:Label ID="lblidProduct" runat="server" Text='<%#Eval("idProduct") %>' />
<br>
<br></br>
<asp:Image ID="img" runat="server" height="160px"
ImageUrl='<%# "~/Img/" + Eval("image") %>' width="160px" />
<br>
<br></br>
<asp:Label ID="lblten" runat="server" Text='<%#Eval("NameProduct") %>' />
<br>
<br></br>
<a href='Detail.aspx?id=<%#Eval("idProduct") %>'>Detail</a>
<br>
<br></br>
<asp:LinkButton ID="addCart" runat="server" onclick="addCart_Click" CommandName="Add" CommandArgument='<%#Eval("idProduct") %>'
Text="Add" />
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
MakeTable();
}
DataTable cart=new DataTable("cart");
void MakeTable()
{
cart.Columns.Add("idProduct");
Session["cart"] = cart;
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
DataRow row;
DataTable tb = new DataTable();
if (e.CommandName == "Add")
{
string product = e.CommandArgument.ToString();
row = cart.NewRow();
row[0] = product;
cart.Rows.Add(row);
Session["cart"] = cart;
}
}
Cart.aspx:
Cart.aspx:
if(Session["cart"]!=null)
{
DataTable tb = (DataTable)Session["cart"];
GridView1.DataSource = tb;
GridView1.DataBind();
}
推荐答案
DataTable dt_Exclusion = new DataTable();
DataColumn ExcCode = new DataColumn("fExcCode", typeof(string));
dt_Exclusion.Columns.Add(ExcCode);
DataColumn ExcDesc = new DataColumn("fExcDesc", typeof(string));
dt_Exclusion.Columns.Add(ExcDesc);
dt_Exclusion.AcceptChanges();
Session["ExclusionTable"] = dt_Exclusion;
然后在该表中添加新行
Then Add a new row in that table
dt_Exclusion = (DataTable)Session["ExclusionTable"];
int Count = dt_Exclusion.Rows.Count;
DataRow dr = dt_Exclusion.NewRow();
dr["fExcCode"] = lnkbtnExcCode.Text;
dr["fExcDesc"] = lnkbtnExcDesc.Text;
dt_Exclusion.Rows.InsertAt(dr, Count);
dt_Exclusion.AcceptChanges();
Session["ExclusionTable"] = dt_Exclusion;
然后绑定Gridview
Then Bind Gridview
dt_Exclusion = (DataTable)Session["ExclusionTable"];
gvresultexclusion.DataSource = dt_Exclusion;
gvresultexclusion.DataBind();
这篇关于将问题从会话表加载到GridView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!