Technology For Your Business For Your Life

Wednesday, September 14, 2011

Set Focus to a control in a modal popup using the ajax control toolkit ModalPopupExtender

I was recently struggling to set focus to a textbox in a Modal Window using the Ajax control toolkit ModalPopupExtender control. This is how to do it.

Use the follow javascript. Make sure this javascript  comes after the asp scriptmanager.

Sys.Application.add_load(modalSetup);
function SetFocusOnControl() {
    document.getElementById('txtEADdress').focus(); // <. control to set focus to.
}
function modalSetup() {
     var modalPopup = $find('ReminderPopUp');             // <. this is the BehaviorID from the ModalPopupExtender
     modalPopup.add_shown(SetFocusOnControl);
}       

Tuesday, September 13, 2011

Email Reporting Services Report as a pdf attachment from C# code

As a follow on from the last post a further requirement we had was for the client to be able to email the .rdlc report as an attachment in pdf format. Here is the code to do that...

     LocalReport localReport = new LocalReport();
            localReport.ReportPath = Server.MapPath("..\\Report\\report.rdlc");
            localReport.EnableExternalImages = true;
            ReportDataSource reportDataSource = new ReportDataSource("DataSet1", q.GetData(ID));
            localReport.DataSources.Add(reportDataSource);
      
          string reportType = "PDF";
          string mimeType;
          string encoding;
          string fileNameExtension;
          string deviceInfo =
    "<DeviceInfo>" +
    "  <OutputFormat>PDF</OutputFormat>" +
    "  <PageWidth>9.5in</PageWidth>" +
    "  <PageHeight>11in</PageHeight>" +
    "  <MarginTop>0.5in</MarginTop>" +
    "  <MarginLeft>1in</MarginLeft>" +
    "  <MarginRight>1in</MarginRight>" +
    "  <MarginBottom>0.5in</MarginBottom>" +
    "</DeviceInfo>";
          Warning[] warnings;
          string[] streams;
          byte[] renderedBytes;

          //Render the report
          renderedBytes = localReport.Render(
              reportType,
              deviceInfo,
              out mimeType,
              out encoding,
              out fileNameExtension,
              out streams,
              out warnings);
          MemoryStream s = new MemoryStream(renderedBytes);
          s.Seek(0, SeekOrigin.Begin);

            BLLEmailComponent email = new BLLEmailComponent();
            email.SendQuote(EmailAddress, s);   //this function call actually contains the code to send the email

Visit us at http://www.creativewebsolutions.co.za/.

Monday, September 12, 2011

View a Reporting Services report in PDF format with the click of a button

I recently had a requirement to allow users of a website  to view their reports in PDF format using Microsoft Reporting Services.
I had the report designed as a .rdlc file but just needed the c# code to send the report straight to pdf.
This is the code I used to solve this task.

LocalReport localReport = new LocalReport();
            localReport.ReportPath = Server.MapPath("..\\Report\\YourReport.rdlc");
            localReport.EnableExternalImages = true;
            ReportDataSource reportDataSource = new ReportDataSource("DataSet1", q.GetData(ID));
            localReport.DataSources.Add(reportDataSource);
    

          string reportType = "PDF";
          string mimeType;
          string encoding;
          string fileNameExtension;
          string deviceInfo =
    "<DeviceInfo>" +
    "  <OutputFormat>PDF</OutputFormat>" +
    "  <PageWidth>9.5in</PageWidth>" +
    "  <PageHeight>11in</PageHeight>" +
    "  <MarginTop>0.5in</MarginTop>" +
    "  <MarginLeft>1in</MarginLeft>" +
    "  <MarginRight>1in</MarginRight>" +
    "  <MarginBottom>0.5in</MarginBottom>" +
    "</DeviceInfo>";
          Warning[] warnings;
          string[] streams;
          byte[] renderedBytes;

          //Render the report
          renderedBytes = localReport.Render(
              reportType,
              deviceInfo,
              out mimeType,
              out encoding,
              out fileNameExtension,
              out streams,
              out warnings);
          Response.Clear();
          Response.ContentType = mimeType;
          Response.AddHeader("content-disposition", "attachment; filename=quote." + fileNameExtension);
          Response.BinaryWrite(renderedBytes);
          Response.End();