2012-11-29 26 views
0

我真的希望有人能幫助我, 我需要從方法GridView1_RowDataBound獲取變量產品到引用值類字符串產品。從一種方法獲取變量並用作私有字符串

它的PDF格式創建者和任何幫助將不勝感激,因爲我一直堅持這一段時間,請原諒,因爲我在.net中相當缺乏經驗。

非常感謝!

enter code here 
public partial class quotedetail : System.Web.UI.Page 
{ 
    private string quoteId; 
    private string product; 
    private string connectId = ""; 
    string description = "Save Your Quote as a PDF with your Company Logo and Print out for Customers"; 

    public string ConnectId { get { return connectId; } } 

    protected void Page_Init(object sender, EventArgs e) 
    { 
     connectId = Global.GetConnectionString(); 
     quoteId = Request.QueryString["quote"]; 
     product = 


    } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      (Master as WebOnlineMasterPage).PageTitle = "Quote - " + quoteId; 


      DisplayPage(); 
     }  
    } 

    protected void DisplayPage() 
    {  
     try 
     { 
      DataSet ds = new WebOnlineQuote.Quote().GetQuote(ConnectId, quoteId); 

      FormView1.DataSource = ds.Tables[0]; 
      GridView1.DataSource = ds.Tables[1]; 




      Page.DataBind(); 
     } 
     catch (System.Web.Services.Protocols.SoapException ex) 
     { 
      Global.CheckException(ex); 
      description = "Sorry, information about this quote is unavailable."; 
      Panel1.Visible = false; 
     } 

     (Master as WebOnlineMasterPage).PageDescription = description; 

    } 

    public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if ((e.Row.RowType == DataControlRowType.DataRow)) 
     { 
      e.Row.Attributes.Add("onmouseover", "className='dataOver'"); 

      if (e.Row.DataItemIndex % 2 == 1) 
       e.Row.Attributes.Add("onmouseout", "className='dataAltRow'"); 
      else 
       e.Row.Attributes.Add("onmouseout", "className='data'"); 

      string product = ((DataRow)((DataRowView)e.Row.DataItem).Row)["ProductCode"].ToString(); 

      e.Row.Attributes.Add("onClick", String.Format("document.location='productdetail.aspx?id={0}'", product)); 


     } 
    } 

    protected void FormView1_DataBound(object sender, EventArgs e) 
    { 
     // get the expiry date 
     bool isopen = (bool)((DataRow)((DataRowView)FormView1.DataItem).Row)["IsOpen"]; 
     bool expired = (bool)((DataRow)((DataRowView)FormView1.DataItem).Row)["Expired"]; 

     int viewIndex = 0; 

     if (isopen && !expired) 
      viewIndex = 1; 

     // set appropriate view 
     MultiView view = (MultiView)FormView1.FindControl("QuoteProcessView"); 

     view.ActiveViewIndex = viewIndex; 
    } 

    protected void AcceptQuote_Btn_Click(object sender, EventArgs e) 
    { 
     Response.Redirect("checkout.aspx?quote=" + quoteId); 
    } 


    protected void Requote_Btn_Click(object sender, EventArgs e) 
    { 
     Response.Redirect("requestquote.aspx?quote=" + quoteId); 
    } 


    // ********************PDF Start******************** 


    protected void SaveAsPdf_Click(object sender, EventArgs e) 
    { 
     DataSet quote = new WebOnlineQuote.Quote().GetQuote(ConnectId, quoteId); 
     Document document = CreateDocument(); 
     PdfDocumentRenderer renderer = new PdfDocumentRenderer(); 
     renderer.Document = document; 
     renderer.RenderDocument(); 
     MemoryStream ms = new MemoryStream(); 
     renderer.PdfDocument.Save(ms); 
     byte[] pdfBytes = ms.ToArray(); 


     Response.Clear(); 
     Response.ContentType = "application/pdf"; 
     Response.AddHeader("content-disposition", "attachment; filename=quote.pdf"); 
     Response.AddHeader("Content-Length", pdfBytes.Length.ToString()); 
     Response.BinaryWrite(pdfBytes); 
     Response.StatusCode = 200; 
     Response.End(); 
    } 

    public Document CreateDocument() 
    { 

     Document document = new Document(); 
     document.Info.Title = "A sample invoice"; 
     document.Info.Subject = "Demonstrates how to create an invoice."; 
     DefineStyles(document); 
     TextFrame addressFrame; 
     MigraDoc.DocumentObjectModel.Tables.Table table; 
     CreatePage(document, out addressFrame, out table); 
     FillContent(document, addressFrame, table); 
     return document; 
    } 

    void DefineStyles(Document document) 
    { 
     // Get the predefined style Normal. 
     MigraDoc.DocumentObjectModel.Style style = document.Styles["Normal"]; 
     // Because all styles are derived from Normal, the next line changes the 
     // font of the whole document. Or, more exactly, it changes the font of 
     // all styles and paragraphs that do not redefine the font. 
     style.Font.Name = "Verdana"; 

     style = document.Styles[StyleNames.Header]; 
     style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right); 

     style = document.Styles[StyleNames.Footer]; 
     style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center); 

     // Create a new style called Table based on style Normal 
     style = document.Styles.AddStyle("Table", "Normal"); 
     style.Font.Name = "Verdana"; 
     style.Font.Name = "Times New Roman"; 
     style.Font.Size = 9; 

     // Create a new style called Reference based on style Normal 
     style = document.Styles.AddStyle("Reference", "Normal"); 
     style.ParagraphFormat.SpaceBefore = "5mm"; 
     style.ParagraphFormat.SpaceAfter = "5mm"; 
     style.ParagraphFormat.TabStops.AddTabStop("16cm", TabAlignment.Right); 
    } 

    void CreatePage(Document document, out TextFrame addressFrame, out MigraDoc.DocumentObjectModel.Tables.Table table) 
    { 

     // Each MigraDoc document needs at least one section. 
     Section section = document.AddSection(); 

     // Put a logo in the header 
     MigraDoc.DocumentObjectModel.Shapes.Image image = section.Headers.Primary.AddImage(Server.MapPath("~/images/cnwlogo.jpg")); 
     image.Height = "2.5cm"; 
     image.LockAspectRatio = true; 
     image.RelativeVertical = RelativeVertical.Line; 
     image.RelativeHorizontal = RelativeHorizontal.Margin; 
     image.Top = ShapePosition.Top; 
     image.Left = ShapePosition.Right; 
     image.WrapFormat.Style = WrapStyle.Through; 

     // Create footer 
     Paragraph paragraph = section.Footers.Primary.AddParagraph(); 
     paragraph.AddText("PowerBooks Inc · Sample Street 42 · 56789 Cologne · Germany"); 
     paragraph.Format.Font.Size = 9; 
     paragraph.Format.Alignment = ParagraphAlignment.Center; 

     // Create the text frame for the address 
     addressFrame = section.AddTextFrame(); 
     addressFrame.Height = "3.0cm"; 
     addressFrame.Width = "7.0cm"; 
     addressFrame.Left = ShapePosition.Left; 
     addressFrame.RelativeHorizontal = RelativeHorizontal.Margin; 
     addressFrame.Top = "5.0cm"; 
     addressFrame.RelativeVertical = RelativeVertical.Page; 

     // Put sender in address frame 
     paragraph = addressFrame.AddParagraph("PowerBooks Inc · Sample Street 42 · 56789 Cologne"); 
     paragraph.Format.Font.Name = "Times New Roman"; 
     paragraph.Format.Font.Size = 7; 
     paragraph.Format.SpaceAfter = 3; 

     // Add the print date field 
     paragraph = section.AddParagraph(); 
     paragraph.Format.SpaceBefore = "8cm"; 
     paragraph.Style = "Reference"; 
     paragraph.AddFormattedText("INVOICE", TextFormat.Bold); 
     paragraph.AddTab(); 
     paragraph.AddText("Cologne, "); 
     paragraph.AddDateField("dd.MM.yyyy"); 

     // Create the item table 
     table = section.AddTable(); 
     table.Style = "Table"; 
     table.Borders.Color = Colors.DarkBlue; 
     table.Borders.Width = 0.25; 
     table.Borders.Left.Width = 0.5; 
     table.Borders.Right.Width = 0.5; 
     table.Rows.LeftIndent = 0; 

     // Before you can add a row, you must define the columns 
     Column column = table.AddColumn("3cm"); 
     column.Format.Alignment = ParagraphAlignment.Left; 

     column = table.AddColumn("5cm"); 
     column.Format.Alignment = ParagraphAlignment.Left; 

     column = table.AddColumn("1cm"); 
     column.Format.Alignment = ParagraphAlignment.Left; 

     column = table.AddColumn("1cm"); 
     column.Format.Alignment = ParagraphAlignment.Left; 

     column = table.AddColumn("2cm"); 
     column.Format.Alignment = ParagraphAlignment.Left; 

     column = table.AddColumn("2cm"); 
     column.Format.Alignment = ParagraphAlignment.Left; 

     column = table.AddColumn("2cm"); 
     column.Format.Alignment = ParagraphAlignment.Right; 

     // Create the header of the table 
     CreateHeaderRow(table); 

     table.SetEdge(0, 0, 7, 1, Edge.Box, MigraDoc.DocumentObjectModel.BorderStyle.Single, 0.75, Color.Empty); 

     //create data table 




    } 

    private static void CreateHeaderRow(MigraDoc.DocumentObjectModel.Tables.Table table) 
    { 
     Row headerRow1 = table.AddRow(); 
     headerRow1.HeadingFormat = true; 
     headerRow1.Format.Alignment = ParagraphAlignment.Center; 
     headerRow1.Format.Font.Bold = true; 
     headerRow1.Shading.Color = Colors.AliceBlue; 
     headerRow1.Cells[0].AddParagraph("PRODUCT"); 
     headerRow1.Cells[0].Format.Font.Bold = false; 
     headerRow1.Cells[0].Format.Alignment = ParagraphAlignment.Left; 
     headerRow1.Cells[0].VerticalAlignment = VerticalAlignment.Bottom; 
     headerRow1.Cells[0].MergeDown = 1; 
     headerRow1.Cells[1].AddParagraph("DESCRIPTION"); 
     headerRow1.Cells[1].Format.Alignment = ParagraphAlignment.Left; 
     headerRow1.Cells[1].MergeDown = 1; 
     headerRow1.Cells[2].AddParagraph("QTY"); 
     headerRow1.Cells[2].Format.Alignment = ParagraphAlignment.Left; 
     headerRow1.Cells[2].VerticalAlignment = VerticalAlignment.Bottom; 
     headerRow1.Cells[2].MergeDown = 1; 

     headerRow1.Cells[3].AddParagraph("UNIT"); 
     headerRow1.Cells[3].Format.Alignment = ParagraphAlignment.Left; 
     headerRow1.Cells[3].VerticalAlignment = VerticalAlignment.Bottom; 
     headerRow1.Cells[3].MergeDown = 1; 

     headerRow1.Cells[4].AddParagraph("PRICE"); 
     headerRow1.Cells[4].Format.Alignment = ParagraphAlignment.Left; 
     headerRow1.Cells[4].VerticalAlignment = VerticalAlignment.Bottom; 
     headerRow1.Cells[4].MergeDown = 1; 

     headerRow1.Cells[5].AddParagraph("TAX"); 
     headerRow1.Cells[5].Format.Alignment = ParagraphAlignment.Left; 
     headerRow1.Cells[5].VerticalAlignment = VerticalAlignment.Bottom; 
     headerRow1.Cells[5].MergeDown = 1; 

     headerRow1.Cells[6].AddParagraph("TOTAL"); 
     headerRow1.Cells[6].Format.Alignment = ParagraphAlignment.Left; 
     headerRow1.Cells[6].VerticalAlignment = VerticalAlignment.Bottom; 
     headerRow1.Cells[6].MergeDown = 1; 


    } 

    class QuoteItemRow 
    { 
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if ((e.Row.RowType == DataControlRowType.DataRow)) 
      { 
       e.Row.Attributes.Add("onmouseover", "className='dataOver'"); 

       if (e.Row.DataItemIndex % 2 == 1) 
        e.Row.Attributes.Add("onmouseout", "className='dataAltRow'"); 
       else 
        e.Row.Attributes.Add("onmouseout", "className='data'"); 

       string product = ((DataRow)((DataRowView)e.Row.DataItem).Row)["ProductID"].ToString(); 

       e.Row.Attributes.Add("onClick", String.Format("document.location='productdetail.aspx?id={0}'", product)); 

      } 
     } 

     public QuoteItemRow(decimal quantity, decimal price, decimal discount, string itemNumber, string title) 
     { 
      Quantity = quantity; 
      Price = price; 
      Discount = discount; 
      ItemNumber = itemNumber; 
      Title = title; 

     } 

     public decimal Quantity; 
     public decimal Discount; 
     public decimal Price; 
     public string ItemNumber; 
     public string Title; 

    } 

    void FillContent(Document document, TextFrame addressFrame, MigraDoc.DocumentObjectModel.Tables.Table table) 
    { 








      Paragraph paragraph = addressFrame.AddParagraph(); 
      paragraph.AddText("Joe Bloggs"); 
      paragraph.AddLineBreak(); 
      paragraph.AddText("12 Some St"); 
      paragraph.AddLineBreak(); 
      paragraph.AddText(product); 



      // Iterate the invoice items 
      decimal totalExtendedPrice = 0; 




      QuoteItemRow[] items = new QuoteItemRow[] { 
       new QuoteItemRow (2.0m, 3.4m, 0.2m, "3213421", product), 
       new QuoteItemRow (2.4m, 3.4m, 0.0m, "3534DD21", quoteId), 
       new QuoteItemRow (8, 0.4m, 0.1m, "908587", quoteId), 

      }; 

      foreach (QuoteItemRow item in items) 
      { 
       decimal quantity = item.Quantity; 
       decimal price = item.Price; 
       decimal discount = item.Discount; 

       // Each item fills two rows 
       Row row1 = table.AddRow(); 
       Row row2 = table.AddRow(); 
       row1.TopPadding = 1.5; 
       row1.Cells[0].Shading.Color = Colors.LightGray; 
       row1.Cells[0].VerticalAlignment = VerticalAlignment.Center; 

       row1.Cells[1].Format.Alignment = ParagraphAlignment.Left; 
       row1.Cells[2].Format.Alignment = ParagraphAlignment.Left; 
       row1.Cells[3].Format.Alignment = ParagraphAlignment.Left; 
       row1.Cells[4].Format.Alignment = ParagraphAlignment.Left; 
       row1.Cells[5].Format.Alignment = ParagraphAlignment.Left; 
       row1.Cells[6].Format.Alignment = ParagraphAlignment.Left; 



       paragraph = row1.Cells[1].AddParagraph(); 
       paragraph.AddFormattedText(item.Title, TextFormat.Bold); 
       row2.Cells[1].AddParagraph(item.Quantity.ToString()); 
       row2.Cells[2].AddParagraph("$" + price.ToString("0.00")); 
       row2.Cells[3].AddParagraph("$" + discount.ToString("0.0")); 
       row2.Cells[4].AddParagraph(); 
       row2.Cells[5].AddParagraph("$" + price.ToString("0.00")); 
       decimal extendedPrice = quantity * price; 
       extendedPrice = extendedPrice * (100 - discount)/100; 
       row1.Cells[5].AddParagraph("$" + extendedPrice.ToString("0.00")); 
       row1.Cells[5].VerticalAlignment = VerticalAlignment.Bottom; 
       totalExtendedPrice += extendedPrice; 

       table.SetEdge(0, table.Rows.Count - 2, 6, 2, Edge.Box, MigraDoc.DocumentObjectModel.BorderStyle.Single, 0.75); 
      } 

      // Add an invisible row as a space line to the table 
      Row row = table.AddRow(); 
      row.Borders.Visible = false; 

      // Add the total price row 
      row = table.AddRow(); 
      row.Cells[0].Borders.Visible = false; 
      row.Cells[0].AddParagraph("Total Price"); 
      row.Cells[0].Format.Font.Bold = true; 
      row.Cells[0].Format.Alignment = ParagraphAlignment.Right; 
      row.Cells[0].MergeRight = 4; 
      row.Cells[5].AddParagraph(totalExtendedPrice.ToString("0.00") + " €"); 

      // Add the VAT row 
      row = table.AddRow(); 
      row.Cells[0].Borders.Visible = false; 
      row.Cells[0].AddParagraph("VAT (19%)"); 
      row.Cells[0].Format.Font.Bold = true; 
      row.Cells[0].Format.Alignment = ParagraphAlignment.Right; 
      row.Cells[0].MergeRight = 4; 
      row.Cells[5].AddParagraph((0.19m * totalExtendedPrice).ToString("0.00") + " €"); 

      // Add the additional fee row 
      row = table.AddRow(); 
      row.Cells[0].Borders.Visible = false; 
      row.Cells[0].AddParagraph("Shipping and Handling"); 
      row.Cells[5].AddParagraph(0.ToString("0.00") + " €"); 
      row.Cells[0].Format.Font.Bold = true; 
      row.Cells[0].Format.Alignment = ParagraphAlignment.Right; 
      row.Cells[0].MergeRight = 4; 

      // Add the total due row 
      row = table.AddRow(); 
      row.Cells[0].AddParagraph("Total Due"); 
      row.Cells[0].Borders.Visible = false; 
      row.Cells[0].Format.Font.Bold = true; 
      row.Cells[0].Format.Alignment = ParagraphAlignment.Right; 
      row.Cells[0].MergeRight = 4; 
      totalExtendedPrice += 0.19m * totalExtendedPrice; 
      row.Cells[5].AddParagraph(totalExtendedPrice.ToString("0.00") + " €"); 

      // Set the borders of the specified cell range 
      table.SetEdge(5, table.Rows.Count - 4, 1, 4, Edge.Box, MigraDoc.DocumentObjectModel.BorderStyle.Single, 0.75); 

      // Add the notes paragraph 
      paragraph = document.LastSection.AddParagraph(); 
      paragraph.Format.SpaceBefore = "1cm"; 
      paragraph.Format.Borders.Width = 0.75; 
      paragraph.Format.Borders.Distance = 3; 
      paragraph.Format.Borders.Color = Colors.DarkBlue; 
      paragraph.Format.Shading.Color = Colors.LightGray; 
     } 

} 
+1

請註明有限的代碼,所以任何一個可以幫助和給予更多的詳細描述你的問題在不同的塊 – Gayatri

+0

你能格式代碼,並指定你卡住了? –

回答

0

最簡單的解決辦法是把在一個會話

Session.Add("ProductCode", ((DataRow)((DataRowView)e.Row.DataItem).Row)["ProductCode"].ToString()); 

值來獲取值:

Session["ProductCode"].ToString() 

但請記住,你是在項目數據綁定這樣做,如果您期望產品代碼有多個值,我建議在將其存儲到會話中之前將其存儲在集合中。

if(Session["ProductCode"] != null) 
{ 
List<string> pcodes = Session["ProductCode"] as List<string>(); 
pcodes.Add(((DataRow)((DataRowView)e.Row.DataItem).Row)["ProductCode"].ToString()); 
Session.Add("ProductCode", pcodes); 
} 
else 
{ 
List<string> pcodes = new List<string>(); 
pcodes.Add(((DataRow)((DataRowView)e.Row.DataItem).Row)["ProductCode"].ToString()); 
Session.Add("ProductCode", pcodes); 
} 

爲了獲取值:

Session["ProductCode"] as List<string>() 
+0

非常感謝jr pineda,我得到了第一部分的工作,但當我嘗試存儲爲列表使用第二段代碼時,我得到的錯誤無法找到類型或名稱空間名稱'列表'(你是否缺少一個使用指令或程序集引用?)再次感謝您的幫助我使用System.Collections.Generic添加了 –

+0

;但現在我得到了無法將默認類型'System.Collections.Generic.List '轉換爲字符串。因爲你是對的,我需要一個以上的產品代碼出現,再次感謝 –

+0

要訪問列表的值,只需使用: pcodes [0] 注意:該列表是一個基於零的索引集合。 –

相關問題