Showing posts with label javaScript. Show all posts
Showing posts with label javaScript. Show all posts

Tuesday, October 30, 2012

asp.net Using WebHandler to generate images on Server Side and send to Client




I had to create dynamic 2D Data Matrix bar code images on the server and send to the client for MeadCo printing.  Found out you can do this by using Web Handlers. 

This is a simple example, the value of the bar code is hardcoded.  Since you can pass query strings from the client to the handler, there is the possibility of creating dynamic barcodes.  This I will be doing using this prototype as a framework.   




WebHandler:

<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Web;
using System.IO;
using System.Drawing;

public class ImageHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {

        //NOTE:  You can get parameters from the client using Query Strings
        
        IEC16022Sharp.DataMatrix dm = new IEC16022Sharp.DataMatrix("<00007>");


        context.Response.ContentType = "image/bmp";
        

        using (Bitmap image = dm.Image)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                ms.WriteTo(context.Response.OutputStream);
            }
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}


Client code:
The code below shows embedded html being generated (This is used for printing).  The option is there just to place an img element within the html document and set it’s src attribute to the name of your handler.




Wednesday, October 17, 2012

JSON serialize with c# deserialize with javascript via pop up window

I created a modal window pop up that shows search results in a gridview. In this gridview there is a checkbox for each row returned. The user can select any rows they want by checking the associated checkbox. One way to send the user's selected data back to the parent window is to use JSON.  Below are code snips on how to to do this.

The c# code serializes the generic list of Icd9 objects into a JSON string.  This new JSON string is sent to a hidden input on the client side. Upon unload of this child window, the window return value is set to the value of the hidden input that was populated on server side. The parent window client code then evals the returned window value.  Once this has been done the value is copied to collection of Icd9 objects.  I for loop my way though this collection accessing the Name and Code properties.



Note:  This works in IE.  I think IE is only browser that supports showModalDialog
           This should work in other browsers if you use winodow.open etc.


Class:
public class Icd9
    {
        public string Name { get; set; }
        public string Code { get; set; }

        public Icd9(string name, string code)
        {
            this.Code = code;
            this.Name = name;
        }

    }


Child Window:
Server side:
using System.Web.Script.Serialization;
 
...
List<CommonTasks.Icd9> selectedCodes = new List<CommonTasks.Icd9>();
 
        for (int i = 0; i < icdGridView.Rows.Count; i++)
        {
            GridViewRow row = icdGridView.Rows[i];
            bool isChecked = ((CheckBox)row.FindControl("selectedRowCb")).Checked;
 
            if (isChecked)
            {
 
                selectedCodes.Add(new CommonTasks.Icd9(icdGridView.Rows[i].Cells[2].Text, icdGridView.Rows[i].Cells[1].Text));
            }
 
 
        }
 
        JavaScriptSerializer js = new JavaScriptSerializer();
        string str = js.Serialize(selectedCodes);
 
        hdnIcd.Value = str;
 
...
 


Client:
 function setWindow_return() {
            var selectedCodes = document.getElementById("hdnIcd");
            window.returnValue = selectedCodes.value;          
        }


Parent Window:
Client:
function GetIcd9()
  {     
    var searchText = document.getElementById("f129").value;
    var url = "Icd.aspx?searchText=" + searchText;
        
    if (searchText.length==0)
        alert(" no value retrieved ");
    else {
          var results = 
           window.showModalDialog(url,"","center:yes;dialogWidth:775px;dialogHeight:480px;resizable:no;status:no;;help:no");
           if(results.length > 0)
           {
             var data = eval(results); 
           
             if(data.length > 0)
             {   
               var icd9Text = "";
               var icd9Code = "";
 
               for(i=0; i < data.length; i++)
               {               
                 icd9Text += data[i].Name + " ";
                 icd9Code += data[i].Code + ",";     
               }
 
               document.getElementById("petsText").value += icd9Text;
               document.getElementById("hidIcd9Code").value += icd9Code;
 
             }
           }
 
         }
  }