Ciao Marco ho riflettuto sulla tua soluzione penso che sia non applicabile perche mi sono spiegato male nel mio primo post. In realtà le tabelle sql che servono per generare la struttura delle tabelle runtime per ogni riga del datagrid sono ben tre: la prima mi da l'intestazione, la seconda query mi da la prima colonna e la combinata tra le due mi danno il corpo della mia tabella contenente i text box.
Il problema di non sapere come fare (e dove) recuperare programmaticamente i valori (.text) dei textbox e come salvarli persiste. Per maggior chiarezza ti posto il mio codice magari sono vicino alla soluzione e neanche lo so:
<asp:datagrid id="GridCarrello" CssClass="Carrello" runat="server" HorizontalAlign="Center" Width="100%" ShowFooter="true"
OnItemDataBound="GridCarrello_ItemDataBound" AutoGenerateColumns="False" CellPadding="3" BorderWidth="1px" BorderStyle="NotSet">
<HeaderStyle CssClass="CarrelloIntestazione" />
<Columns>
<asp:BoundColumn DataField="ProductId" Visible="false" />
<asp:TemplateColumn ItemStyle-Width="20%" HeaderText="ARTICOLO">
<ItemTemplate>
<asp:label id="productname" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.productname") %>' />
<a id="LnkPreview" runat="server" class="highslide" onclick="javascript:return hs.expand(this, {captionId: 'caption1'})">
<img src="" id="ImgPreview" runat="server" alt="Clicca per zoomare il modello" />
</a>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn ItemStyle-Width="65%" HeaderText="COLORE/TAGLIA">
<ItemTemplate>
<asp:PlaceHolder id="PlhVarianti" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn ItemStyle-Width="5%" HeaderText="QUANTITA'">
<ItemTemplate>
<asp:TextBox id="Quantity" CssClass="InputClass" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.quantity") %>' Columns="3">
</asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="QuantityEdit" CssClass="InputClass" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.quantity") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn ItemStyle-Width="5%" DataField="UnitPrice" HeaderText="PREZZO" />
<asp:ButtonColumn ItemStyle-Width="5%" Text="elimina" CommandName="Delete" />
</Columns>
</asp:datagrid>
<asp:label id="Label3" runat="server" Font-Bold="True">Total Amount :</asp:label><asp:label id="lblAmt" runat="server" Font-Bold="True"></asp:label></TD>
<asp:button id="Button1" runat="server" Text="Recalculate"></asp:button>
e poi
string IDUser;
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.GridCarrello.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.GridCarrello_DeleteCommand);
//this.GridCarrello.SelectedIndexChanged += new System.EventHandler(this.GridCarrello_SelectedIndexChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
//this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
protected void Page_PreRender(object sender, EventArgs e)
{
}
protected void Page_Load(object sender, EventArgs e)
{
IDUser = SecurityManager.SecurityManager.GetUserID(User.Identity.Name.ToString());
if (!Page.IsPostBack)
{
FillCartFromDb();
}
}
private void FillCartFromDb()
{
DataSet ds = ShoppingCart.CShoppingCart.GetAll(IDUser);
GridCarrello.DataSource = ds;
GridCarrello.DataBind();
Button1_Click(null, null);
}
private void Button1_Click(object sender, System.EventArgs e)
{
decimal total = 0;
try
{
foreach (DataGridItem dgi in GridCarrello.Items)
{
if (dgi.ItemType == ListItemType.Item || dgi.ItemType == ListItemType.AlternatingItem)
{
TextBox t = (TextBox)dgi.Cells[3].Controls[1];
int quantity = int.Parse(t.Text);
decimal unitprice = Decimal.Parse(dgi.Cells[4].Text);
total = total + (unitprice * quantity);
ShoppingCart.CShoppingCart.UpdateQuantity(Session.SessionID, int.Parse(dgi.Cells[0].Text), quantity, IDUser);
}
}
}
catch
{
}
lblAmt.Text = total.ToString();
FillCartFromDb();
}
public void GridCarrello_ItemDataBound(object sender, DataGridItemEventArgs e)
{
DataRow Row;
string UID;
string idproduct;
string idshoppingcart;
HtmlImage ImgPreview;
HtmlAnchor LnkPreview;
PlaceHolder PlhVarianti;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem || e.Item.ItemType == ListItemType.EditItem)
{
//CONFIG DATAGRID STYLE
//e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#DDEEFF'");
//e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
//e.Item.Style["cursor"] = "hand";
Row = ((DataRowView)(e.Item.DataItem)).Row;
UID = Row["IDShoppingCart"].ToString();
idproduct = Row["ProductId"].ToString();
idshoppingcart = Row["IDShoppingCart"].ToString();
ImgPreview = ((HtmlImage)(e.Item.Cells[1].FindControl("ImgPreview")));
LnkPreview = ((HtmlAnchor)(e.Item.Cells[1].FindControl("LnkPreview")));
PlhVarianti = ((PlaceHolder)(e.Item.Cells[1].FindControl("PlhVarianti")));
//RECUPERO L'IMMAGINE DEL MODELLO
SqlConnection conn;
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WFDB"].ConnectionString);
string SelectImgDefault = "SELECT * FROM V_PRODUCT_MEDIA WHERE IDProduct = " + idproduct + "";
SqlCommand CmdImgDefault = new SqlCommand(SelectImgDefault, conn);
conn.Open();
SqlDataReader ObjDRImgDefault = CmdImgDefault.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if (ObjDRImgDefault.Read())
{
System.Drawing.Image realImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream((byte[])ObjDRImgDefault["Thumbnail"]));
int altezza = realImage.Height * Convert.ToInt32(ConfigurationManager.AppSettings["FattoreScala"]) / 100;
int lunghezza = realImage.Width * Convert.ToInt32(ConfigurationManager.AppSettings["FattoreScala"]) / 100;
ImgPreview.Height = altezza;
ImgPreview.Width = lunghezza;
ImgPreview.Src = "Image.aspx?idproduct=" + idproduct + "&IdImage=" + ObjDRImgDefault["IdImage"].ToString() + "&ScaleFactor=" + ConfigurationManager.AppSettings["FattoreScalaDefault"] + "";
LnkPreview.HRef = "Image.aspx?idproduct=" + idproduct + "&IdImage=" + ObjDRImgDefault["IdImage"].ToString() + "&ScaleFactor=" + ConfigurationManager.AppSettings["FattoreScalaDefault"] + "";
}
ObjDRImgDefault.Close();
conn.Close();
//**********************************************************
//COSTRUZIONE MATRICE VARIANTI
//**********************************************************
Table VariantTable = new System.Web.UI.WebControls.Table();
VariantTable.BorderWidth = 1;
VariantTable.CssClass = "Varianti";
VariantTable.GridLines = GridLines.Both;
VariantTable.CellPadding = 0;
VariantTable.CellSpacing = 0;
VariantTable.Width = Unit.Percentage(98);
//REPERIMENTO DEI COLORI
SqlConnection conn_color;
conn_color = new SqlConnection(ConfigurationManager.ConnectionStrings["WFDB"].ConnectionString);
string SelectColor = "SELECT StyleAssociations.Product AS IDProduct, StyleAssociations.Color AS IDColor, Colors.Name, Colors.Red, Colors.Green, Colors.Blue FROM StyleAssociations INNER JOIN Colors ON StyleAssociations.Color = Colors.Id GROUP BY StyleAssociations.Color, StyleAssociations.Product, Colors.Name, Colors.Red, Colors.Green, Colors.Blue HAVING (StyleAssociations.Product = " + idproduct + ")";
SqlCommand CmdColor = new SqlCommand(SelectColor, conn_color);
conn_color.Open();
SqlDataReader ObjDRColor = CmdColor.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
TableRow tr = new TableRow();
//PRIMA CELLA VUOTA
TableCell tdv = new TableCell();
tdv.Width = Unit.Percentage(10);
tdv.Text = "";
tdv.VerticalAlign = VerticalAlign.Middle;
tr.Cells.Add(tdv);
ArrayList ListColor = new ArrayList();
while (ObjDRColor.Read())
{
ListColor.Add(ObjDRColor["IDColor"].ToString());
TableCell td = new TableCell();
td.Width = Unit.Percentage(10);
td.Text = /*ObjDRColor["Name"].ToString().ToLower() + */ "<div style='background-color: " + System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(Int32.Parse(ObjDRColor["Red"].ToString()), Int32.Parse(ObjDRColor["Green"].ToString()), Int32.Parse(ObjDRColor["Blue"].ToString()))) + "; width: 10px; height: 10px;'></div>";
td.VerticalAlign = VerticalAlign.Middle;
tr.Cells.Add(td);
}
ObjDRColor.Close();
conn_color.Close();
//INTESTAZIONE DEI TOTALI DELLE QUANTITA'
TableCell tdIntTotal = new TableCell();
tdIntTotal.Width = Unit.Percentage(10);
tdIntTotal.Text = "TOTALE";
tdIntTotal.VerticalAlign = VerticalAlign.Middle;
tr.Cells.Add(tdIntTotal);
//AGGIUNGO LE CELLE COLORI ALLA TABELLA
VariantTable.Rows.Add(tr);
//REPERIMENTO DELLE TAGLIE
SqlConnection conn_size;
conn_size = new SqlConnection(ConfigurationManager.ConnectionStrings["WFDB"].ConnectionString);
string SelectSize = "SELECT StyleAssociations.Product AS IDProduct, StyleAssociations.Size AS IDSize, Sizes.Name AS Size FROM StyleAssociations INNER JOIN Sizes ON StyleAssociations.Size = Sizes.Id GROUP BY StyleAssociations.Product, StyleAssociations.Size, Sizes.Name HAVING (StyleAssociations.Product = " + idproduct + ")";
SqlCommand CmdSize = new SqlCommand(SelectSize, conn_size);
conn_size.Open();
SqlDataReader ObjDRSize = CmdSize.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
while (ObjDRSize.Read())
{
TableRow trsize = new TableRow();
TableCell tdsize = new TableCell();
tdsize.Width = Unit.Percentage(10);
tdsize.Text = ObjDRSize["Size"].ToString().ToUpper();
tdsize.VerticalAlign = VerticalAlign.Middle;
trsize.Cells.Add(tdsize);
//REPERIMENTO DELLE QUANTITA'
for (int j = 0; j < ListColor.Count; j++)
{
TableCell tdquantity = new TableCell();
tdquantity.Width = Unit.Percentage(10);
SqlConnection conn_quantity;
conn_quantity = new SqlConnection(ConfigurationManager.ConnectionStrings["WFDB"].ConnectionString);
string SelectQuantity = "SELECT * FROM STYLEASSOCIATIONS WHERE Product = " + idproduct + " AND COLOR=" + ListColor[j].ToString() + " AND SIZE=" + ObjDRSize["IDSize"].ToString() + "";
SqlCommand CmdQuantity = new SqlCommand(SelectQuantity, conn_quantity);
conn_quantity.Open();
SqlDataReader ObjDRQuantity = CmdQuantity.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if (ObjDRQuantity.Read())
{
if (ObjDRQuantity["Quantity"] != DBNull.Value)
{
tdquantity.Text = ObjDRQuantity["Quantity"].ToString(); /* + "<input type=text size=3 class=InputClass>"; */
//TODO: DA PROVARE
ArrayList ListQuantityCode = new ArrayList();
string QuantityCode = "";
SqlConnection connQuantityCode;
connQuantityCode = new SqlConnection(ConfigurationManager.ConnectionStrings["WFDB"].ConnectionString);
string SelectQuantityCode = "SELECT ASPNETQUANTITYCODE FROM SHOPPINGCART_PRODUCTS WHERE IDShoppingCart = " + idshoppingcart + "";
SqlCommand CmdQuantityCode = new SqlCommand(SelectQuantityCode, connQuantityCode);
connQuantityCode.Open();
SqlDataReader ObjDRQuantityCode = CmdQuantityCode.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if (ObjDRQuantityCode.Read())
{
if (ObjDRQuantityCode["ASPNETQUANTITYCODE"] != DBNull.Value)
{
ListQuantityCode.Add(ObjDRQuantityCode["ASPNETQUANTITYCODE"].ToString());
}
}
ObjDRQuantityCode.Close();
connQuantityCode.Close();
TextBox qty = new TextBox();
qty.ID = "Crr_" + ListColor[j].ToString() + "_" + ObjDRSize["IDSize"].ToString();
//DEVO RECUPERARE QUI LA PROCEDURA PER SALVARE I VALORI DEI MIEI TEXT BOX?
//DEVO EFFETTUARE QUI LA PROCEDURA PER SALVARE I VALORI DEI TEXTBOX?
qty.Text = "";
qty.CssClass = "InputClass";
qty.Width = 30;
tdquantity.Controls.Add(qty);
}
else
{
tdquantity.Text = "";
}
}
ObjDRQuantity.Close();
conn_quantity.Close();
tdquantity.VerticalAlign = VerticalAlign.Middle;
trsize.Cells.Add(tdquantity);
}
//CALCOLO TOTALI DELLE QUANTITA' PER COLOR/SIZE
TableCell tdtotal = new TableCell();
tdtotal.Width = Unit.Percentage(10);
int TotaleQuantita = 0;
for (int i = 0; i < ListColor.Count; i++)
{
SqlConnection conn_totalquantity;
conn_totalquantity = new SqlConnection(ConfigurationManager.ConnectionStrings["WFDB"].ConnectionString);
string SelectTotalQuantity = "SELECT * FROM STYLEASSOCIATIONS WHERE Product = " + idproduct + " AND COLOR=" + ListColor[i].ToString() + " AND SIZE=" + ObjDRSize["IDSize"].ToString() + "";
SqlCommand CmdTotalQuantity = new SqlCommand(SelectTotalQuantity, conn_totalquantity);
conn_totalquantity.Open();
SqlDataReader ObjDRTotalQuantity = CmdTotalQuantity.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
while (ObjDRTotalQuantity.Read())
{
if (ObjDRTotalQuantity["Quantity"] != DBNull.Value)
{
TotaleQuantita += Int32.Parse(ObjDRTotalQuantity["Quantity"].ToString());
tdtotal.Text = TotaleQuantita.ToString();
}
}
ObjDRTotalQuantity.Close();
conn_totalquantity.Close();
}
tdtotal.VerticalAlign = VerticalAlign.Middle;
//END TOTALI DELLE QUANTITA' PER COLOR/SIZE
trsize.Cells.Add(tdtotal);
//AGGIUNGO LE CELLE SIZE ALLA TABELLA
VariantTable.Rows.Add(trsize);
}
ObjDRSize.Close();
conn_size.Close();
//AGGIUNGO AL PLACEHOLDER LA TABELLA VARIANTI
PlhVarianti.Controls.Add(VariantTable);
//**********************************************************
//END COSTRUZIONE MATRICE VARIANTI
//**********************************************************
}
}
private void GridCarrello_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
ShoppingCart.CShoppingCart.DeleteItem(Session.SessionID, int.Parse(e.Item.Cells[0].Text), IDUser);
FillCartFromDb();
}