Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Datagrid contenente tabella a runtime

    Salve a tutti, da qualche tempo ho un gran bel problema che cercherò di spiegare, e ne sarò davvero grato a chi me lo risolvesse o mi desse utili indicazioni su come operare.

    Ho una pagina ASP.NET 2.0 così strutturata: un datagrid contenente delle colonne di tipo itemtemplate (1 colonna immagine- 2 colonna label, 3 colonna placeholder - 4 colonna label) i cui valori sono letti da database e la cui logica è piazzata nell'evento ONITEMDATABOUND, e fin qui nessun problema. Sempre nel medesimo evento costruisco a runtime una tabella (3 colonna del datagrid di tipo itemtemplate contenete un placeholder) avente il numero di righe e il numero di colonne dipendenti da due differenti query; sempre all'interno di tale tabella le celle contengono dei textbox creati anch'essi runtime secondo alcune condizioni dettate dalle query precedenti. Tale tabella ASP.NET è aggiunta runtime al placeholder, il quale come già detto, è piazzato nella 3 colonna itemtemplate del datagrid, ed anche fin qui nessun problema.

    Adesso arrivano i seri problemi.
    Il primo problema nasce dal fatto che tali textbox, contenuti nella tabella asp.net e costruiti a runtime devono, all'avvio della pagina, essere valorizzati da valori prelevati da una tabella sql (che non so come strutturare perchè come detto in precedenza il numero di textbox è variabile in funzione delle query precedenti) ed inoltre non so che logica scrivere per effettuare tale operazione e dove piazzarla (mi viene da pensare sempre nell'ONITEMDATABOUND del datagrid).

    Il secondo problema nasce quando devo andare ad aggiornare il database contenente i valori dei textbox eventualmente modificati dall'utente tramite un click di un pulsante.

    E' una struttura abbastanza articolata, infatti sono arenato e non so come proseguire in questa struttura.
    AIUTO

  2. #2
    Per maggior chiarezza posto il mio codice:

    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.ConnectionStrin gs["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.CommandBeh avior.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.ConnectionStrin gs["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.Drawi ng.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.ConnectionStrin gs["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.ConnectionStrin gs["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.CommandBehav ior.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.ConnectionStrin gs["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.CommandB ehavior.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 EFFETTUARE QUI LA PROCEDURA PER RECUPERARE 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.ConnectionStrin gs["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.Command Behavior.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
    //************************************************** ********
    }
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.