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 { getset; }
        public string Code { getset; }

        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;
 
             }
           }
 
         }
  }

No comments: