February 20, 2010

Iterating over JSON object (string ) with jquery

We had a scenario where in we need to search for particular object in collection. Whenever the user changes something in dropdown in ASP.NET we need to show some value in a label. If we take the drop down value to server it would take time.... so what we thot is we searialized the object using JavascriptSerializer in ASP.NET and converted to JSON string.. passsed to a javascript function to search.... (whenever dropdown changes)... I was literally amazed with the power of JSON and jquery combination... thot i would share with people in need.....

Here is the JSON string...in Javascript...

var obj = [{"Description":"No jquery pricing group may be used for Testing", "ID":1, "TempID":1, "CompareID":3}, {"Description":"No jquery pricing group may be used for Testing", "ID":2, "TempID":2, "CompareID":3}, {"Description":"No jquery pricing group may be used for Testing", "ID":3, "TempID":3, "CompareID":3}, {"Description":"No jquery pricing group may be used for Testing", "ID":4, "TempID":4, "CompareID":3}, {"Description":"No jquery pricing group may be used for Testing", "ID":5, "TempID":1, "CompareID":4}, {"Description":"Jquery required for pricing Testing", "ID":6, "TempID":2, "CompareID":4}, {"Description":"Jquery required for pricing Testing", "ID":7, "TempID":3, "CompareID":4}, {"Description":"Jquery required for pricing Testing", "ID":8, "TempID":4, "CompareID":4}, {"Description":"No jquery pricing group may be used for Testing", "ID":9, "TempID":1, "CompareID":5}, {"Description":"Jquery required for pricing Testing", "ID":10, "TempID":2, "CompareID":5}, {"Description":"Jquery required for pricing Testing", "ID":11, "TempID":3, "CompareID":5}, {"Description":"Jquery required for pricing Testing", "ID":12, "TempID":4, "CompareID":5}];

We had a requirement to searhch the description for matching TempID and COmpareID...

Our Javascript function was amazingly...

function Myfunc(ID1,Exp1){jQuery.each(obj, function Test(x) { if (obj[x].TempID == ID1 && obj[x].CompareID == Exp1) //return obj[x].Description; alert(obj[x].Description);}); }

This would have helped you to understand the power of jqeury

January 16, 2010

ASP.NET Dynamic User controls

Today after long time...I moved to an assignment to work in ASP.NET after the gap of almost nealry 5 years. I started working on an Intranet Portal for a financial institution.

We wanted to generate certain UIs dynamically, we chose different user controls will be loaded based on the user requirement. One thing I learned today is "Dynamic Controls Should be Loaded on Each PostBack" On every postback literally you need to load the controls again and again. If you want the control to have its Viewstate data preserved, adding the USercontrols to page (Parent) should happen before LoadViewstate event gets fired in the page life cycle.

Normally the user controls will be added as Controls.Add(UsercontrolPath), where ocntrols is Place holder in the page/another user control.

I will write more about this assignement that looks very interesting.