Wednesday, March 11, 2009

Modifying form data during unload – does it ever work?

There might arise some requirement to change the form data being posted using JavaScript. So naturally, the first attempt would be to use unload event on the window/document. And then in the event handler, change the form data.

It would be done, if using jQuery, in the following manner.

   1: $(window).unload(function()


   2: {


   3:    addAndSet("#formValue");


   4: });


   5:  


   6:  


   7: //function that adds an option to selectbox.


   8: //also sets the newly added value as the selected one!


   9: function addAndSet(item)


  10: {


  11:     var current = $(item);


  12:     current.append($('<option></option>').


  13:                        val('-214783').


  14:                        html('defaulted').


  15:                        attr('selected','true')


  16:                    );


  17:     current.removeAttr('disabled'); 


  18:     //the current should not disabled.


  19:     // if disabled, the selection does not apply.


  20: }




The above script also shows how to add a new OPTION to a HTML Select Box (DropDownList/Listbox/ComboBox). It sets the value as a fixed one and html as fixed too. And then it sets the “selected” attribute to true. You can modify this method based on your requirement. Note that it uses jQuery so you should be including the appropriate jQuery.js file. It also removes the disabled attribute since the selection would not work if the control was disabled.



Would this work? If not, why?


Well! No. The reason is – the unload on the window would be invoked and by that time the form data is already posted to the server. So your goal to change the form data would not be met since the data is already posted. I have tested with document.unload as well and the result is still the same.



Workaround?


This would be the workaround that worked for my situation – I had a submit button which would be clicked by the user to submit the form and then then some checks has to be performed. So I invoked the addAndSet in the click event of the submit button and then it works like a charm. This makes sense since the event handler is executed before the form data is posted.



My code looks something like this.





   1: $("#actReport").click( function () {                 


   2:                  var itName = document.forms[0].itName;


   3:                  var itGroup = document.forms[0].itGroup;


   4:                  if(itName.value.length > 0){


   5:                    //add option to itGroup


   6:                    addAndSet("#itGroup");                   


   7:                  }


   8:                  else if(itGroup.value.length > 0){


   9:                    addAndSet("#itName");                   


  10:                  }     


  11:             } 


  12:         );




Shown above in the lines 2-3 is the way you could access your form data using traditional JavaScript.



Note that if its a listbox, then the “value” property does not return all the values selected. Instead, it just returns the first selection in your box. For my purpose, checking the length works. If no option is selected the length would be 0 and if some option is selected, the length be the “length of the value(number of characters in the value)”.

Getting the login name for the current process

On Windows. a process always run using some credentials. The following method should work with Windows applications as well as the XBAP.

   1: var x = WindowsIdentity.GetCurrent();


   2: x.Name gives you the current user.


Monday, March 09, 2009

IE Links not changing colors

For documentation purpose.

These days I am doing more web design than any development and part which involves me to change the styles (its a big pain in the butt).

I had an issue with hyperlinks not  changing colors when clicked/visited. My requirement was to keep the colors the same irrespective of their state.

My CSS was something like shown..

   1: .mainTableHeaders a:link,.mainTableHeaders a:hover,.mainTableHeaders a:visited,.mainTableHeaders a:active


   2: {


   3:     color:white;


   4: }




But the change of color on the links were not showing up.



To fix this, just clear the IE History and Temporary Files. And you should be good to go.



Very soon, I would start posting screen casts for a .NET course that I am conducting for a bunch of my friends. At that time I would also cover a simple jQuery tutorial.

Wednesday, February 25, 2009

Binding dynamic types in WPF using IronPython and DLR


What would be covered in this post?

- Creating classes in IronPython and comparing with C# classes. Adding public properties in IronPython class.

- Using classes/interfaces in IronPython class which is in the hosting assembly. For example, if the application hosting the DLR and executing the IronPython class at runtime has an interface IDyn, this article covers on how to use that interface within the Python script.

- Accessing the dynamic type created in the IronPython and making an instance of it.

- Finally, binding the dynamic type to WPF controls.

Pre-Requisites

Before, we get started, I recommend you go through the article I posted previously which shows how to use the IronPython engine within .NET applications. I will be using the same PythonEngine class with a new method added as shown below.

   1: public T GetDynamicType<T>(string scriptFile)


   2:         {


   3:             try


   4:             {


   5:                 var source = engine.CreateScriptSourceFromFile(scriptFile);


   6:                 source.Execute(scope);


   7:                 var SomeClass = engine.GetVariable(scope, "DynObject");


   8:                 var t = engine.Operations.Create(SomeClass);


   9:                 return (T)t;


  10:             }


  11:             catch (Exception ex)


  12:             {


  13:                 Debug.WriteLine(ex);


  14:             }


  15:             return default(T);


  16:         }





In the above method, we see that the engine (refer to the previous article, it would be useful if you want to understand it) creates an instance of ScriptSource object from the script file. Then we pass the current ScriptScope we created earlier and pass it to the Execute() method which would make the class be available to be used. Line 7 shows using GetVariable() on the engine and we pass the scope (which knows where the class we are looking for is) and also the name of the class, which is DynObject. Once we have the class (I assume its the IL, or at least i understood it that way), we use the Create() method on the engine passing the class which would return instance of that class. Then we simply return the instance by casting it to type T (a generic, so we use default(T) which is like null but for generics). So this method basically executes a python script and returns an instance of the class we specify. May be I would just refactor the method to make it look like shown below and which allows the method itself to be more generic than it is.





   1: public T GetDynamicType<T>(string scriptFile, string className)


   2:         {


   3:             try


   4:             {


   5:                 var source = engine.CreateScriptSourceFromFile(scriptFile);


   6:                 source.Execute(scope);


   7:                 var SomeClass = engine.GetVariable(scope, className);


   8:                 return (T)engine.Operations.Create(SomeClass);


   9:             }


  10:             catch (Exception ex)


  11:             {


  12:                 Debug.WriteLine(ex);


  13:             }


  14:             return default(T);


  15:         }




The method is used as shown below.





   1: object o = pe.GetDynamicType<Object>("DynObject.py", "DynObject");




Creating IronPython class with public properties



Consider the C# class shown below.





   1: public class DynObject : IDyn 


   2: {


   3:    public string Name { get; set; }


   4:    public string Age  { get; set; }


   5: }




The class is named DynObject which implements the interface IDyn and which has two public properties Name and Age. The python code for the same is shown below.





   1: class DynObject(IDyn):


   2:   


   3:   def __init__(self): # this is the constructor


   4:     self._name = None # initialized to NULL


   5:     self._age = None # initialized to NULL


   6:     


   7:   def __getName(self):


   8:     return self._name


   9:     


  10:   def __getAge(self):


  11:     return self._age  


  12:     


  13:   def __setName(self,value):


  14:     self._name = value


  15:     


  16:   def __setAge(self,value):


  17:     self._age = value


  18:     


  19:   Name = property( # this is the Name property. use "property"


  20:      fget= __getName, # getter


  21:      fset= __setName  # setter


  22:   )


  23:   


  24:   Age = property(


  25:      fget=__getAge,


  26:      fset=__setAge


  27:   )






The statement followed by # are the comments. Hope they help you relate the classes properly and with that one should be able to write basic classes on their own.



Importing Namespaces in IronPython and setting up default namespaces



Now the problem with the above script when you Execute() it in the method I first discussed is that it would complain about not knowing what IDyn is. So basically we do an import the namespace. But the interface IDyn was created inside a namspace “Dynamics” which is the same application that is executing the python script. Had it been a ‘Debug’ then one could have done the following to import System.Diagnostics.*





   1: import clr


   2: clr.AddReference("System.Diagnostics")


   3: from System.Diagnostics import *


   4: # from System.Diagnostics import Debug 


   5: # if you just want to import Debug class




But the issue I has was to know what assembly should I mention in the AddReference and I was wondering if there was a good way to import some default namespaces into the engine without having the user mention them manually. For that reason we have ScriptRuntime.LoadAssembly(Assembly asmToLoad) which can be used to add reference to assemblies of your choice. In order to support this, I felt that it would be a good idea to load the assemblies before you even generate the ScriptSource object from the script file. For this reason, I modified the PythonEngine (the wrapper I talked about in my previous post on IronPython) constructor and now it looks like as shown.





   1: private PythonEngine(){


   2:             engine = Python.CreateEngine(new Dictionary<string, object>() { 


   3:                 { 


   4:                 "DivisionOptions", PythonDivisionOptions.New 


   5:                 } //using Dictionary Initializer ;)    


   6:             });


   7:             var runtime = engine.Runtime;


   8:             //Load the assemblies into the engine's runtime.


   9:             runtime.LoadAssembly(typeof(Debug).Assembly);


  10:             runtime.LoadAssembly(typeof(string).Assembly);


  11:             runtime.LoadAssembly(typeof(IDyn).Assembly);


  12:             scope = engine.CreateScope();


  13: }




What should be of interest to import default assemblies into the system are in lines 7-11. Then on we can move on to add the imports to the python file as shown. No more AddReference() required.





   1: from System.Diagnostics import Debug


   2: from Dynamics import IDyn


   3:  


   4: class DynObject(IDyn):


   5:   


   6:   def __init__(self):


   7:     Debug.WriteLine("here")


   8:     self._name = "buddi"


   9:     self._age = "22"


  10:     


  11:   def __getName(self):


  12:     return self._name


  13:     


  14:   def __getAge(self):


  15:     return self._age  


  16:     


  17:   def __setName(self,value):


  18:     self._name = value


  19:     


  20:   def __setAge(self,value):


  21:     self._age = value


  22:     


  23:   Name = property(


  24:      fget= __getName,


  25:      fset= __setName


  26:   )


  27:   


  28:   Age = property(


  29:      fget=__getAge,


  30:      fset=__setAge


  31:   )




Note that you still need to do the “from  NAMESPACE import CLASSNAME/*”.



How about integrating the python code in WPF?



The IDyn was just an empty interface which was added to explain how one could use .NET interfaces within IronPython classes. So back to the WPF application, which is simple and straight forward. The XAML code for the Window is shown below.





   1: <Window x:Class="DynamicBinding.Window1"


   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"


   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"


   4:     Title="Window1" Height="300" Width="300">


   5:     <StackPanel>


   6:         <TextBlock Text="{Binding Name}" Background="Red" Margin="5" Height="30"/>


   7:         <TextBlock Text="{Binding Age}" Background="Blue" Margin="5" Height="30"/>


   8:     </StackPanel>


   9: </Window>




From the bindings shown above, the Name property is looked for in the DataContext of TextBlock, StackPanel or Window. So in the code-behind we use our PythonEngine to obtain an instance of the dynamic type we created and set the DataContext of the window to the instance we obtained. The code behind is shown below.





   1: using System;


   2: using System.Windows;


   3: using Dynamics;


   4:  


   5: namespace DynamicBinding


   6: {


   7:     /// <summary>


   8:     /// Interaction logic for Window1.xaml


   9:     /// </summary>


  10:     public partial class Window1 : Window


  11:     {


  12:         private PythonEngine pe = PythonEngine.Engine;


  13:  


  14:         public Window1()


  15:         {


  16:             InitializeComponent();


  17:             object o = pe.GetDynamicType<Object>("DynObject.py", "DynObject");


  18:             this.DataContext = o;


  19:         }


  20:     }


  21: }




This ability to bind WPF controls to dynamic types would be a great feature if you would like to generate UI based on the user controlled params. This would be even easier with the dynamic support in C# 4.0 which I hope to see another release during the MIX 2009.



Next I would like to work on generate the python classes from XML which would be provided by the user and even the XAML would be generated from the XML. This would be the basis for my reporting suite which would be powered by Silverlight, DLR and WCF.