看看这个,用STATIC变量来保存数据,提交的时候移除。
--CS
using System
using System.Data
using System.Configuration
using System.Collections
using System.Web
using System.Web.Security
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.WebControls.WebParts
using System.Web.UI.HtmlControls
using System.Collections.Generic
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["A"] = "AAA"
}
}
//通过sessionid能获取维一的客户段内容
protected void addyearPro_Click(object sender, EventArgs e)
{
string SessionID = Session.SessionID
string Name = NAME.Text
string Qty = NUM.Text
Product pc = new Product(Name, Qty)
List<Product>newPList = ManagerProcut.Find(SessionID)
if (newPList != null)
{
newPList.Add(pc)
}
else
{
newPList = new List<Product>()
newPList.Add(pc)
ManagerProcut.pc.Add(SessionID, newPList)
}
GridView1.DataSource = newPList
GridView1.DataBind()
}
//来临时保存客户端的内容
public static class ManagerProcut
{
public static Dictionary<string, List<Product>>pc = new Dictionary<string, List<Product>>()
public static List<Product>Find(string Key)
{
if (pc.ContainsKey(Key))
{
return pc[Key]
}
else
{
return null
}
}
public static bool Remove(string Key)
{
if (pc.ContainsKey(Key))
{
return pc.Remove(Key)
}
else
{
return false
}
}
}
public class Product
{
public Product(string ProductName,string Qty)
{
this._ProductName = ProductName
this._Qty = Qty
}
private string _ProductName
private string _Qty
public string ProductName
{
get { return _ProductName}
set { _ProductName = value}
}
public string Qty
{
get { return _Qty}
set { _Qty = value}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
Response.Write("<p>新记录:")
foreach (TableCell tc in gr.Cells)
{
Response.Write("<br/>")
Response.Write(tc.Text)
}
Response.Write("</p>")
}
string SessionID = Session.SessionID
ManagerProcut.pc.Remove(SessionID)//移除
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String Sessionid = Session.SessionID
GridViewRow gr = GridView1.Rows[e.RowIndex]
string ProductName = gr.Cells[0].Text
List<Product>per = ManagerProcut.Find(Sessionid)
if (per.Count >0)
{
Product pdt = null
foreach (Product pr in per)
{
if (pr.ProductName == ProductName)
{
pdt = pr
break
}
}
if (pdt != null)
{
bool m = per.Remove(pdt)
if (m)
{
GridView1.DataSource = per
GridView1.DataBind()
}
}
}
}
_aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http //www w3 org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http //www w3 org/1999/xhtml">
<head id="Head1" runat="server">
<title>未命名页面</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="185px" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField HeaderText="产品名称" DataField="PRoductName" />
<asp:BoundField HeaderText="产品数量" DataField="QTY" />
<asp:ButtonField HeaderText ='Delete' CommandName="DELETE" Text="Delete"/>
</Columns>
</asp:GridView>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="11%" style="height: 24px">
<asp:TextBox ID="NAME" runat="server" /></td>
<td width="14%" style="height: 24px">
<asp:TextBox ID="NUM" runat="server" /></td>
<td width="75%" style="height: 24px">
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="增加" OnClick="addyearPro_Click" /></td>
</tr>
</table>
<asp:Button ID="Button2" runat="server" Text="提交" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
可以使用a=gridview1.rows.count获得gridview的行数,再用gridview1.rows[a].cells[1]="P20"就可以加进去了。以上可能有误,不过思路是这样。可以取出gridview的值,可以修改gridview的值,问题就简单了gridview中动态插入一个gridview控件?
我们一般开发不会用GridView套用GridView 因为GridView绑定的都是列.
取行在绑定下个gridview控件做的话很复杂。
用Repeater+GridView来做,第一层用Repeater数据.
同时第一层的数据通过查询绑定下个GridView中即可
前台 *** 作:
<asp:Repeater ID="Repeater1" DataSourceID="SqlDataSource1" runat="server"onitemdatabound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<thead>
<td>编号</td>
<td>名称</td>
<td>类型</td>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id")%>'>
</asp:Label>
</td>
<td><%# Eval("name")%></td>
<td><%# Eval("type")%></td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
后台 *** 作:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e){
if (!e.Item.ItemIndex.Equals(-1))
{
GridView gvInfo = (GridView)e.Item.FindControl("GridView2")
string id = (e.Item.FindControl("Label1") as Label).Text
gvInfo.DataSource = SQLDBHelper.CreateIntance().GetDataSet("select * from Tab where id=" + id)
gvInfo.DataBind()
Response.Write(id)
}
}
效果图如下外层Repeater 内层通过外层编号绑定GridView:
如有问题可以追问,我当及时回答.
希望能帮到你!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)