Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi.....
when i selected value="Pdf" it executes successfully but after that when am going to select Value="Word" event is not fired (also written autopostback="true")


below is the code any body knows pl....z Reply......




C#
protected void ddl_chanched(object sender, EventArgs e)
       {

               if (DropDownList1.SelectedValue == "Pdf")
               {
                   Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                   PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                   pdfDoc.Open();
                   using (MemoryStream stream = new MemoryStream())
                   {
                       Chart1.SaveImage(stream, ChartImageFormat.Png);
                       iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
                       string lbl1 = Label1.Text.ToString();
                       pdfDoc.Add(new Paragraph(lbl1));
                       chartImage.ScalePercent(75f);
                       pdfDoc.Add(chartImage);
                       pdfDoc.Close();

                       Response.ContentType = "application/pdf";
                       Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf");
                       Response.Cache.SetCacheability(HttpCacheability.NoCache);
                       Response.Write(pdfDoc);

                       Response.End();








                   }


           }
           else if (DropDownList1.SelectedValue == "Word")
           {

                   Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
                   string tmpChartName = "test2.jpg";
                   string imgPath = HttpContext.Current.Request.PhysicalApplicationPath + tmpChartName;

                   Chart1.SaveImage(imgPath);
                   string imgPath2 = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + tmpChartName);

                   Response.Clear();
                   Response.ContentType = "application/vnd.ms-word";
                   Response.AddHeader("Content-Disposition", "attachment; filename=test.doc;");
                   StringWriter stringWrite = new StringWriter();
                   HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                   string headerTable = @"<table><tr><td><img src='" + imgPath2 + @"' \></td></tr></table>";
                   Response.Write(headerTable);
                   Response.Write(stringWrite.ToString());
                   Response.End();
               }
           }
           }
Posted
Updated 12-Sep-12 19:54pm
v2
Comments
Kuthuparakkal 13-Sep-12 1:50am    
Put break point on ddl_chanched event and examine the Dropdown selected value, you may have some spaces or something different from "Word" like "word" or any such case sensitive issue.
madhuri@mumbai 13-Sep-12 2:10am    
hello mr, he says that event not fired then after selecting word,so how you can say that "Word" different than "word",event not fired here so,its doesn't go inside.

Your dropdown is firing the event. Only thing is it is not going inside word block(else...if block). It may be because of string matching. May be you have given value in small case and checking in different case. In this kind of situations always use breakpoint and try stepping through the code.
Try this:
C#
protected void ddl_chanched(object sender, EventArgs e)
{    
    if (DropDownList1.SelectedValue.ToUpper == "PDF")
    {
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        using (MemoryStream stream = new MemoryStream())
        {
            Chart1.SaveImage(stream, ChartImageFormat.Png);
            iTextSharp.text.Image chartImage = iTextSharp.text.Image.GetInstance(stream.GetBuffer());
            string lbl1 = Label1.Text.ToString();
            pdfDoc.Add(new Paragraph(lbl1));
            chartImage.ScalePercent(75f);
            pdfDoc.Add(chartImage);
            pdfDoc.Close(); 
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Chart.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);                        
            Response.End();
        }       
    }
    else if (DropDownList1.SelectedValue.ToUpper() == "WORD")
    {       
        Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
        string tmpChartName = "test2.jpg";
        string imgPath = HttpContext.Current.Request.PhysicalApplicationPath + tmpChartName;

        Chart1.SaveImage(imgPath);
        string imgPath2 = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + tmpChartName);

        Response.Clear();
        Response.ContentType = "application/vnd.ms-word";
        Response.AddHeader("Content-Disposition", "attachment; filename=test.doc;");
        StringWriter stringWrite = new StringWriter();
        HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        string headerTable = @"<table><tr><td><img src="" + imgPath2 + @""></img></td></tr></table>";
        Response.Write(headerTable);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
    DropDownList1.AutoPostBack = true;
} 


Hope it helps.
--Amit
 
Share this answer
 
v2
Comments
Ashok Guntoju 13-Sep-12 2:21am    
not case sensitive issues man even first i selected Word it executes successfully after that am selected Pdf its not fired..........
_Amy 13-Sep-12 2:29am    
Check my updated answer. Try adding DropDownList1.AutoPostBack = true; at the end of the if...else block.
When your Dropdown selected index change at that time first page_load event fired so put the break point at page_load event and check if here you set selected index become 0 or first of the dropdown values.
 
Share this answer
 
Hi,
The problem is because of response object that ends on every block of condition . There is no place to return true to make control do post-back.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900