Friday, October 2, 2009

CallingSilverlightFromASPXPage

While the page load and to populate silverlight control, in Page_Load() of .aspx following has to be done.
multiFileUploaderSilverlightControl.InitParameters = string.Format( "FileName={0},SnapshotImage1={1}", "MultiFileUploader", snapshotImage);

 

To access the values from the above passed method, in the .xaml page’s constructor,


public MultiFileUploader(IDictionary<string, string> param)
{
   InitializeComponent();
   HtmlPage.RegisterScriptableObject(param["FileName"], this);
   if (param.Count > 0)
   {
      _snapShotImage1 = param["SnapshotImage1"];
   }
}
To call a specific usercontrol from the silverlight application, the name has to set in the InitParameters, in this case “MultiFileUploader” and this will be checked in the App.xaml.cs as shown below,
private void Application_Startup(object sender, StartupEventArgs e)
{
  if (e.InitParams != null)
  {
     foreach(var item in e.InitParams)
     {
         Resources.Add(item.Key, item.Value);
     }
  }
   if (Current.Resources.Contains("FileName"))
   {
      fileName = Convert.ToString(Current.Resources["FileName"]);
   }
   if (!string.IsNullOrEmpty(fileName))
   {
      switch (fileName)
      {
         case "MultiFileUploader":
            this.RootVisual = new MultiFileUploader(e.InitParams);
            break;
      }
   }
}

 
To call a method from .aspx page to .xaml(silverlight) page, the following has to be done.
First decorate the class in the xaml page with the attribute [ScriptableType] as
[ScriptableType]
public partial class MultiFileUploader : UserControl
{   }
and decorate the method with the attribute [ScriptableMember]

[ScriptableMember]
public void UploadFiles(string param)
{   }

so that this method can be called from .aspx’s javascript method as shown below
var silverligthControl = document.getElementById('multiFileUploaderSilverlightControl'); silverligthControl.Content.MultiFileUploader.UploadFiles(param);
where, multiFileUploaderSilverlightControl is silverlight control added in .aspx page and in the next line we have called
SilverlightControlName.Content.ClassName.MethodName(parameter)

To call a method from .xaml page to .aspx page, the following has to be done.
HtmlPage.Window.Invoke("ValueFromMultiFileUploaderXaml", param);
In the above line we invoke ValueFromMultiFileUploaderXaml Javascript method which is in .aspx page

To access the values passed from the above javascript method call,

function ValueFromMultiFileUploaderXaml(fileDetails)
{
   if (fileDetails.length > 0)
   {
      var param = fileDetails[0];
   }
}

Thursday, September 17, 2009

AccessingQuerystringFromJavascript

I recently came across a situation to access query string from javascript.

Here is the method to access query string keys and values.
Say for this URL http://www.testsite.co.in/?Mode=ADD
the following method,

var queryString = window.location.search.substring(1);

fetches querystring.

Now the variable 'queryString' will have "Mode=ADD".

Thursday, August 6, 2009

ControlPopulationError

I added UIElements whose value is maintained across the application level, inside the Stack panel container control which is in Page_1.xaml. After leaving from Page_1.xaml to Page_2.xaml and again navigating to Page_1.xaml caused me an error while populating the global content to the stack panel. The error I got is,

"Specified element is already the logical child of another element. Disconnect if first."

The error was due to, at the first time population of stack panel went happily

downloadStackPanel.Children.Add(videoDownloadStatus);

but at the second time when I tried to populate the global content to the stack panel, the previous instance of stack panel still holds the global content and prevented me to populate the same content further and caused an exception.

I tried lot many workarounds for populating the stack panel the second time and later found the solution to be too simple.

In Page_1.xaml I created Closing = "Window_Closing" event. This event will be fired when the current window is closed. In this event I cleared the stack panel

downloadStackPanel.Children.Clear()

so no more global content will be there before I go to new window(Page_2.xaml). Now if I go back to the Page_1.xaml the population of stack panel will happen with no exception as the contents are already cleared.

Friday, March 6, 2009

DebuggerDisplayAttribute

During debugging sometime we fed up to see value of a property if it is deep inside a tree structure. We have to keep on click the “+” sign to expand the tree to reach our deep inside located property. On frequent debug it becomes a hectic task. DebuggerDisplayAttribute comes as a survivor.

DebuggerDisplayAttribute Class determines how a class or field is displayed in the debugger variable windows. This attribute has a single argument: a string to be displayed in the value column for instances of the type. This string can contain braces ({ and }). The text within a pair of braces is evaluated as the name of a field, property, or method. For example, the following C# code causes "MyTechnology = “.NET” " to be displayed when the cursor is placed on family variable.


Here I need to check the value assigned to Technology property frequently, so I declared that property in DebuggerDisplayAttribute. Futher the class containing that property should be decorated by DebuggerDisplayAttribute.

This attribute can be applied to the following:
Classes
Structures
Delegates
Enumerations
Fields
Properties
Assemblies


The picture is shown below.


Tuesday, February 10, 2009

Extension method

Extension method is a new feature available in .NET 3.0 framework. Extension Method enable a variety of useful scenarios, and help make possible the really powerful LINQ query framework that is being introduced with .NET as part of the VS 2008 / .NET 3.5 release. The usage is shown in the below snapshot.



Beware to simply insert the this keyword before the int argument in the Double() helper method, which make the extension method possible, then we can indeed type side.Double(). In fact, the advantage of doing this is that intellisense will help you by showing the method in the dropdown when you type a dot after the variable and also makes something more readable. The extension method should be static and class as well.
However extension method should be avoided in cases where encapsulation breaks.

Monday, February 9, 2009

Inheritance in WPF

Completely new to Windows Presentation Foundation (WPF). It was quite interesting to learn it. During coding I was happened to use inheritance. As usual in any ‘C’ coding style programming language I followed the pattern BaseClass : DerivedClass in my .xaml pages. I was getting build error until I discover the additional add-on code. The code snippet is shown below.


Instead of the Window or UserControl tag, we need to use Src tag followed by the file name (here PageBase, for my PageBase.cs file). To make that file to be recognized we need to specify the namespace too, which is done in xmlns:src property. This piece makes inheritance to happen.

Thursday, February 5, 2009

Manage Control Visibility Using CSS in Server Side

There are some situations we make our controls visibility to false in our page load and try to access that very same control in JavaScript. Since its visibility is set false, that control wont be rendered in the HTML and the poor JavaScript could not find that element.
Instead of using ‘Visible’ property we can use CSS to get that work done. It will make the control invisible and at the same time, using JavaScript, we can able to find that control as well. Code snippet shown below.

Friday, January 2, 2009

Disable debug property in Web.config

While deploying an Asp.net application in production server be sure to change the debug attribute to false in the compilation tag in Web.config. If it is set to true, it will be slowly eating more memory and the performance of the application will be compromised. Few controls like TreeView, Menu won't be cached if the debug is set to true and for this reason these controls will be downloaded for every request and some unnecessary debug codes will get executed there by slow down the application.

There may be chances to forget to change the property, and to avoid that, in Asp.net 2.0 we can use machie.config file by adding the deployment tag. The syntax is shown below.


The default value is false and it is added only in machine.config and not in application level. After setting to true, it disables the output trace and turn off the detailed error message, so that hackers cant dig much into the internals of the application. This will ensure the application gives best performance and avoid security leaks.