<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dot Net Thoughts &#187; Javascript</title>
	<atom:link href="http://www.dotnetthoughts.net/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Pass your own arguments to the ClientValidationFunction in a CustomValidator</title>
		<link>http://www.dotnetthoughts.net/2010/05/30/pass-your-own-arguments-to-the-clientvalidationfunction-in-a-customvalidator/</link>
		<comments>http://www.dotnetthoughts.net/2010/05/30/pass-your-own-arguments-to-the-clientvalidationfunction-in-a-customvalidator/#comments</comments>
		<pubDate>Sun, 30 May 2010 09:41:04 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[C#.Net]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=896</guid>
		<description><![CDATA[Last few days I am working on an ASP.Net, where I can create Grids, Dropdown lists with Post back , and the best part is browser compatibility, client only requires compatible with IE. Yes it was a refresher course for me, I revisited GridView templates, Page.IsPostback checking etc. Today I got a requirement like I [...]]]></description>
			<content:encoded><![CDATA[<p>Last few days I am working on an ASP.Net, where I can create Grids, Dropdown lists with Post back <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , and the best part is browser compatibility, client only requires compatible with IE. Yes it was a refresher course for me, I revisited GridView templates, Page.IsPostback checking etc. Today I got a requirement like I need to validate few radio buttons. I searched for an implementation, I found we can use required field validator for RadioButtonList, but in my case it was Radio buttons (Initially it was RadioButtonList, but due to some customization issues, I changed it to Radio Buttons). Then I implemented a custom validator, and in the client validation function I hardcoded the Radio Button ids, because for the client validation function we can&#8217;t pass the parameters to the function. Same problem again I faced in a GridView, where the problem I found was I won&#8217;t get the client id of the radio button in the Item template. <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  After doing some research I found we can extend the custom validator control using Validation Framework. And here is the implementation. Using this we can add properties to the source / sender parameter in the client side validation function.</p>
<pre class="brush: jscript;">
function ValidateRadioButtons(source, arguments) {
    var radio1 = document.getElementById(source.Radio1);
    var radio2 = document.getElementById(source.Radio2);
    var radio4 = document.getElementById(source.Radio4);
    var radio3 = document.getElementById(source.Radio3);
    var result = radio1.checked || radio2.checked || radio3.checked || radio4.checked;
    arguments.IsValid = result;
}
</pre>
<p>This is the JavaScript function which implements the validation logic. You can get the controls as part of the Source object. To get the Source.Radio1, you need to register the Radio1 as ClientValidator attribute. You can do some C# code like this.</p>
<pre class="brush: csharp;">
protected void Page_Load(object sender, EventArgs e)
{
    this.Page.ClientScript.RegisterExpandoAttribute(customCheck.ClientID, &quot;Radio1&quot;, this.radio1.ClientID, false);
    this.Page.ClientScript.RegisterExpandoAttribute(customCheck.ClientID, &quot;Radio2&quot;, this.radio2.ClientID, false);
    this.Page.ClientScript.RegisterExpandoAttribute(customCheck.ClientID, &quot;Radio3&quot;, this.radio3.ClientID, false);
    this.Page.ClientScript.RegisterExpandoAttribute(customCheck.ClientID, &quot;Radio4&quot;, this.radio4.ClientID, false);
}
</pre>
<p>And the ASPX Code is like this</p>
<pre class="brush: xml;">
&lt;asp:CustomValidator runat=&quot;server&quot; ID=&quot;customCheck&quot; ClientValidationFunction=&quot;ValidateRadioButtons&quot;
        ErrorMessage=&quot;Choose any option&quot; /&gt;
</pre>
<p>And here is the Output code generated for the custom validator.</p>
<div id="attachment_899" class="wp-caption aligncenter" style="width: 304px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/05/custom_validator_rendering.png"><img src="http://www.dotnetthoughts.net/wp-content/uploads/2010/05/custom_validator_rendering.png" alt="Custom validator output rendering" title="Custom validator output rendering" width="294" height="68" class="size-full wp-image-899" /></a><p class="wp-caption-text">Custom validator output rendering</p></div>
<p>Happy Coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/05/30/pass-your-own-arguments-to-the-clientvalidationfunction-in-a-customvalidator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>System.InvalidOperationException &#8211; The length of the string exceeds the value set on the maxJsonLength property.</title>
		<link>http://www.dotnetthoughts.net/2010/04/16/system-invalidoperationexception-the-length-of-the-string-exceeds-the-value-set-on-the-maxjsonlength-property/</link>
		<comments>http://www.dotnetthoughts.net/2010/04/16/system-invalidoperationexception-the-length-of-the-string-exceeds-the-value-set-on-the-maxjsonlength-property/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 15:00:51 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=854</guid>
		<description><![CDATA[Yesterday I got a problem from one of my friend; he is getting a Javascript exception from some Ajax Page methods. The exception occurred when he tried to return a very long string from web method. The exception was like this. Error: Sys.Net.WebServiceFailedException: The server method &#8216;WebServiceMethod&#8217; failed with the following error: System.InvalidOperationException&#8211; Error during [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I got a problem from one of my friend; he is getting a Javascript exception from some Ajax Page methods. The exception occurred when he tried to return a very long string from web method. The exception was like this.</p>
<p>Error: Sys.Net.WebServiceFailedException: The server method &#8216;WebServiceMethod&#8217; failed with the following error: System.InvalidOperationException&#8211; Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.</p>
<p>After doing some searching we resolved it. You can resolve this error by modifying the web.config file, and add / modify the Configuring JSON Serialization.</p>
<pre class="brush: xml;">
&lt;configuration&gt;
  &lt;system.web.extensions&gt;
    &lt;scripting&gt;
      &lt;webServices&gt;
        &lt;jsonSerialization maxJsonLength=&quot;5000&quot;/&gt;
      &lt;/webServices&gt;
    &lt;/scripting&gt;
  &lt;/system.web.extensions&gt;
&lt;/configuration&gt;
</pre>
<p>The default length is 102400.<br />
You can get more information about this from MSDN : http://msdn.microsoft.com/en-us/library/bb763183.aspx</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/04/16/system-invalidoperationexception-the-length-of-the-string-exceeds-the-value-set-on-the-maxjsonlength-property/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing simple Windows 7 Gadget &#8211; Part 3</title>
		<link>http://www.dotnetthoughts.net/2010/03/09/developing-simple-windows-7-gadget-part-3/</link>
		<comments>http://www.dotnetthoughts.net/2010/03/09/developing-simple-windows-7-gadget-part-3/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 18:50:11 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Windows Vista]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=795</guid>
		<description><![CDATA[In this I am exploring another two features of Gadget API, one is Docking and other is Flyout. The Docking feature is helpful in Windows Vista, because the gadget lives only in Vista Sidebar, but in Windows 7 it can be anywhere in the desktop. The Docking feature helps to change the size of Gadget, [...]]]></description>
			<content:encoded><![CDATA[<p>In this I am exploring another two features of Gadget API, one is Docking and other is Flyout. The Docking feature is helpful in Windows Vista, because the gadget lives only in Vista Sidebar, but in Windows 7 it can be anywhere in the desktop. The Docking feature helps to change the size of Gadget, in Windows Vista, the docking / un-docking events will automatically fired, when we drag the gadget out of the sidebar, but in Windows 7 another button will displayed in the Gadget toolbox. You can query the current state of a gadget with System.Gadget.docked. It returns true if docked, false if undocked.</p>
<div id="attachment_798" class="wp-caption alignnone" style="width: 170px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/03/image.png"><img class="size-full wp-image-798" title="Docking / UnDocking button in Sidebar" src="http://www.dotnetthoughts.net/wp-content/uploads/2010/03/image.png" alt="Docking / UnDocking button in Sidebar" width="160" height="273" /></a><p class="wp-caption-text">Docking / UnDocking button in Sidebar</p></div>
<p>There are also two events that you can monitor, System.Gadget.onDock and System.Gadget.onUndock, to determine when the docking state changes.</p>
<p>You can change the Width of the gadget by calling <em>document.body.style.width = &#8220;130px&#8221;;</em>. And the Docking and Undocking events should be attached on the Page_load.</p>
<pre class="brush: jscript;">
System.Gadget.onDock = dockStateChanged;
System.Gadget.onUndock = dockStateChanged;
</pre>
<p>And the dockStateChanged function is</p>
<pre class="brush: jscript;">
function dockStateChanged() {
//Checks the current state of the Gadget, if Docked state, setting the width to 130px;
//Otherwise changing to 330px;
	if (System.Gadget.docked) {
		document.body.style.width = &quot;130px&quot;;
document.getElementById(&quot;RSSOutput&quot;).style.width = &quot;125px&quot;;
	} else {
		document.body.style.width = &quot;330px&quot;;
document.getElementById(&quot;RSSOutput&quot;).style.width = &quot;325px&quot;;
	}
}
</pre>
<p>Another feature is Flyout. Flyouts used to extend the Gadget UI. The flyouts are system modal—only one can be displayed at a time. And when a gadget loses focus, its flyout will close. API for Flyouts available under System.Gadget.Flyout. As the flyouts is completely a separate window, with its own DOM, helps to add / create controls dynamically in it. Also you need to specify the flyout file, like Settings file.</p>
<p>The System.Gadget.Flyout.document returns the Document object of the flyout window. To display a flyout you need to set Flyout.show to True and to hide, set it to false. This property also returns the current state of flyout. But we cannot control the location of side bar, it is managed by Sidebar or OS.</p>
<pre class="brush: jscript;">
System.Gadget.Flyout.file = &quot;flyout.html&quot;;
</pre>
<p>There are certain events also available for Flyouts</p>
<pre class="brush: jscript;">
System.Gadget.Flyout.onShow = function()
{
//Do something, when flyout is opening, may be a dynamic control creation
}
System.Gadget.Flyout.onHide = function()
{
//Do some code.
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/03/09/developing-simple-windows-7-gadget-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing simple Windows 7 Gadget &#8211; Part 2</title>
		<link>http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-2/</link>
		<comments>http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-2/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 18:05:39 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Gadget]]></category>
		<category><![CDATA[Gadget API]]></category>
		<category><![CDATA[Sidebar Gadget]]></category>
		<category><![CDATA[Windows Vista]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=787</guid>
		<description><![CDATA[In my last post I have created a simple Windows 7 Gadget for testing Regular expressions. In this post we look into the Gadget Options dialog and Gadget API, which helps to display the options dialog and save the preferences from the User. The options dialog is a nice feature used in Gadgets to manage [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-1/">last post </a>I have created a simple Windows 7 Gadget for testing Regular expressions. In this post we look into the Gadget Options dialog and Gadget API, which helps to display the options dialog and save the preferences from the User. The options dialog is a nice feature used in Gadgets to manage the user preferences. It is also an HTML file. Unlike the main Gadget HTML file, which is specified in the XML Definition file, the Options HTML File is specified in the main gadget file or in the main Javascript file, generally in the gadget initialization area of your script. In the RegEx Tester example, I am using the RegEx function flags as customization options.</p>
<div id="attachment_790" class="wp-caption alignnone" style="width: 240px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/settings_clicked.png"><img class="size-full wp-image-790" title="RegEx Tester Settings " src="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/settings_clicked.png" alt="RegEx Tester Settings " width="230" height="327" /></a><p class="wp-caption-text">RegEx Tester Settings</p></div>
<p>So I have created an HTML File with 3 checkboxes and wrote a javascript function to get the user selected options, create the flags variable, and when closing the Options dialog, save the user preferences. To create the settings or options dialog, set the SettingsUI property.  </p>
<pre class="brush: jscript;">
System.Gadget.settingsUI = &quot;settings.html&quot;;
</pre>
<p>After setting the SettingsUI property, the Gadget will display a Options button in the Gadget handle.</p>
<div id="attachment_789" class="wp-caption alignnone" style="width: 240px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/RegExTester_Settings.png"><img src="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/RegExTester_Settings.png" alt="Settings Button" title="Settings Button " width="230" height="280" class="size-full wp-image-789" /></a><p class="wp-caption-text">Settings Button </p></div>
<p>The Gadget API also supports various events / callback functions for the Options dialog, like SettingsClosing, SettingsClosed etc, which helps to save the Preferences and Read the preferences in the Gadget.</p>
<pre class="brush: jscript;">
System.Gadget.onSettingsClosed = settingsClosed;
function settingsClosed(event) {
	//OK Button clicked.
if (event.closeAction == event.Action.commit) {
	//Load settings here.
}
}
</pre>
<p>Gadget API supports various methods to read and write settings. For writing setting we can use System.Gadget.Settings.write or System.Gadget.Settings.writeString. Both the functions will expects a key/value pair. And for reading can use System.Gadget.Settings.read or System.Gadget.Settings.readString, both of these functions take a key and return a value. If the key does not exist (for instance if it has never been written) both will return a value of undefined. Use System.Gadget.Settings.xxxxString method, if you are working with string values. All the settings are stored in Settings.ini file. And it is available in the &#8220;C:\Users\\AppData\Local\Microsoft\Windows Sidebar&#8221; location.<br />
Saving the preferences</p>
<pre class="brush: jscript;">
System.Gadget.Settings.write(&quot;RegExOptionsSaved&quot;, &quot;true&quot;);
System.Gadget.Settings.write(&quot;RegExOption&quot;, escape(result));
</pre>
<p>And reading the Preferences </p>
<pre class="brush: jscript;">
if (System.Gadget.Settings.read(&quot;RegExOptionsSaved&quot;)) {
regExOptions = unescape(System.Gadget.Settings.read(&quot;RegExOption&quot;));
}
</pre>
<p><strong>RegEx Object constructor</strong> </p>
<pre class="brush: jscript;">
var re = new RegEx(&quot;Pattern&quot;,&quot;Flags&quot;);
</pre>
<p>And the Flags are</p>
<ol>
<li>
Global Search &#8211; g &#8211; The global search flag makes the RegExp search for a pattern throughout the string, creating an array of all occurrences it can find matching the given pattern.</li>
<li>Ignore Case &#8211; i &#8211; The ignore case flag makes a regular expression case insensitive. For international coders, note that this might not work on extended characters such as å, ü, ñ, æ.</li>
<li>Multiline Input &#8211; m &#8211; This flag makes the beginning of input (^) and end of input ($) codes also catch beginning and end of line respectively.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing simple Windows 7 Gadget &#8211; Part 1</title>
		<link>http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-1/</link>
		<comments>http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-1/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 14:50:19 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Gadget]]></category>
		<category><![CDATA[RegEx]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[Sidebar Gadget]]></category>
		<category><![CDATA[Windows Vista]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=777</guid>
		<description><![CDATA[Gadgets are simple and lightweight applications, which can be developed using HTML, Javascript and CSS. Gadgets can also include Image files. In Windows Vista, we can create Gadgets by zipping the files, and rename the it with .Gadget extension. In Windows 7 we can create Gadget by creating the .Gadget folder in C:\Program Files\Windows Sidebar\Gadgets\ [...]]]></description>
			<content:encoded><![CDATA[<p>Gadgets are simple and lightweight applications, which can be developed using HTML, Javascript and CSS. Gadgets can also include Image files. In Windows Vista, we can create Gadgets by zipping the files, and rename the it with .Gadget extension. In Windows 7 we can create Gadget by creating the .Gadget folder in C:\Program Files\Windows Sidebar\Gadgets\ folder. This folder contains all the default gadgets from Microsoft.</p>
<p><strong>Creating Simple RegEx Tester Gadget</strong></p>
<p>Regular Expressions are complex but efficient technique for processing text. And the RegEx functions are available in Javascript. So I am to creating simple regex tester gadget using Javascript. You can find a simple RegEx Tester using Javascript here : <a href="http://www.regular-expressions.info/javascriptexample.html" target="_blank">http://www.regular-expressions.info/javascriptexample.html</a>.</p>
<p>For creating a Gadget we require a XML Definition file, which contains the details about the Gadget File, Links to Icons, and Copyright information. Here is skeleton a XML Definition file.</p>
<pre class="brush: xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;gadget&gt;
&lt;name&gt;RegExTester&lt;/name&gt;
&lt;namespace&gt;dotnetthoughts&lt;/namespace&gt;
&lt;version&gt;1.0.0.0&lt;/version&gt;
&lt;author name=&quot;dotnetthoughts&quot;&gt;
&lt;info url=&quot;http://dotnetthoughts.net&quot; text=&quot;www.dotnetthoughts.net&quot; /&gt;
&lt;logo src=&quot;logo.png&quot; /&gt;
&lt;/author&gt;
&lt;copyright&gt;&amp;#0169; 2010&lt;/copyright&gt;
&lt;description&gt;For Testing Regular Expressions&lt;/description&gt;
&lt;icons&gt;
&lt;icon width=&quot;64&quot; height=&quot;64&quot; src=&quot;icon.png&quot; /&gt;
&lt;/icons&gt;
&lt;hosts&gt;
&lt;host name=&quot;sidebar&quot;&gt;
&lt;base type=&quot;HTML&quot; apiVersion=&quot;1.0.0&quot; src=&quot;RegExTester.html&quot; /&gt;
&lt;permissions&gt;full&lt;/permissions&gt;
&lt;platform minPlatformVersion=&quot;0.3&quot; /&gt;
&lt;/host&gt;
&lt;/hosts&gt;
&lt;/gadget&gt;
</pre>
<p>And the Tags details are</p>
<p>name: Title of your gadget.<br />
version: Version number of your gadget.<br />
author: Your name or your company’s name.<br />
info url: Web site address.<br />
info text: Friendly name for your Web site.<br />
logo src: Name of company’s logo image file.<br />
copyright: Copyright notice.<br />
description: Description of the gadget.<br />
icon src: Name of icon image file for the gadget.<br />
base src: Name of gadget’s main HTML file.</p>
<div id="attachment_780" class="wp-caption alignnone" style="width: 469px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/details.png"><img class="size-full wp-image-780" title="Gadget Details." src="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/details.png" alt="Gadget Details." width="459" height="300" /></a><p class="wp-caption-text">Gadget Details.</p></div>
<p>Save this file as Gadget.xml. Next we need to create the RegExTester.html, which is the main file, nothing but simple HTML file with some UI components.</p>
<pre class="brush: xml;">

&lt;html&gt;
&lt;head&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;style.css&quot; /&gt;
&lt;script type=&quot;text/vbscript&quot; src=&quot;message.vbs&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;regexcheck.js&quot;&gt;&lt;/script&gt;
&lt;title&gt;RegEx Tester&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;1&quot; cellspacing=&quot;5&quot; bordercolor=&quot;red&quot;&gt;
&lt;tr&gt;&lt;td&gt;&lt;b&gt;&lt;u&gt;RegEx Tester&lt;/u&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;label&gt;Regular Expression&lt;/label&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;textarea id=&quot;txtRegExp&quot; cols=&quot;25&quot; rows=&quot;4&quot;&gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;label&gt;Subject String&lt;/label&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;textarea id=&quot;txtSubject&quot; cols=&quot;25&quot; rows=&quot;4&quot;&gt;&lt;/textarea&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;button onclick=&quot;javascript:demoMatchClick();&quot;&gt;Test Match&lt;/button&gt;&amp;nbsp;&amp;nbsp;
&lt;button onclick=&quot;javascript:demoShowMatchClick();&quot;&gt;Show Match&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>You may noticed that I have included a message.vbs file, it helps to display alert() boxes from Javascript. I don&#8217;t know why alert and prompt functions are not available in Gadgets. Also we don&#8217;t need to worry about cross platform compatibility issues in Gadgets, we can write anything supported by Microsoft Internet Explorer.</p>
<p>Here is the code of Message.vbs file.</p>
<pre class="brush: vb;">
sub alert(prompt)
MsgBox prompt, 48 , &quot;RegEx Tester&quot;
end sub
</pre>
<p>And here is the contents of regexcheck.js file.</p>
<pre class="brush: jscript;">
function demoMatchClick() {
var regExCtrl = document.getElementById(&quot;txtRegExp&quot;);
var subjectCtrl = document.getElementById(&quot;txtSubject&quot;);
var re = new RegExp(regExCtrl.value);
if (subjectCtrl.value.match(re)) {
	alert(&quot;Successful match&quot;);
} else {
	alert(&quot;No match&quot;);
}
}
function demoShowMatchClick() {
var regExCtrl = document.getElementById(&quot;txtRegExp&quot;);
var subjectCtrl = document.getElementById(&quot;txtSubject&quot;);
var re = new RegExp(regExCtrl.value);
var m = re.exec(subjectCtrl.value);
if (m == null) {
	alert(&quot;No match&quot;);
} else {
	var s = &quot;Match at position &quot; + m.index + &quot;:\n&quot;;
	for (i = 0; i &lt; m.length; i++) {
		s = s + m[i] + &quot;\n&quot;;
	}
	alert(s);
}
}
</pre>
<p>Also I added a CSS file for styling the components. Also to specify the Height and Width of the Gadget. In Windows Vista, we need to limit the size of the Gadget, because the Gadget can only place in the Sidebar, but in Windows 7 we can place the Gadget anywhere in the desktop. Also I have added icon.png and logo.png files, as specified in the XML definition file.<br />
The current folder structure is like this.</p>
<div id="attachment_781" class="wp-caption alignnone" style="width: 600px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/folder_struct.png"><img class="size-full wp-image-781" title="Folder Structure" src="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/folder_struct.png" alt="Folder Structure" width="590" height="166" /></a><p class="wp-caption-text">Folder Structure</p></div>
<p>Now using any compression tool, compress the files, and Rename it as RegExTester.Gadget. Double click on the Gadget file, Windows will ask for a confirmation.</p>
<div id="attachment_779" class="wp-caption alignnone" style="width: 424px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/confirm.png"><img class="size-full wp-image-779" title="Gadget install Confirmation" src="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/confirm.png" alt="Gadget install Confirmation" width="414" height="266" /></a><p class="wp-caption-text">Gadget install Confirmation</p></div>
<p>Click Install. It will start the Gadget. Here is the screenshot of RegExTester gadget running on my Windows 7 machine.</p>
<div id="attachment_782" class="wp-caption alignnone" style="width: 240px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/RegExTester_Running.png"><img class="size-full wp-image-782" title="RegExTester Running on Windows 7" src="http://www.dotnetthoughts.net/wp-content/uploads/2010/02/RegExTester_Running.png" alt="RegExTester Running on Windows 7" width="230" height="280" /></a><p class="wp-caption-text">RegExTester Running on Windows 7</p></div>
<p>This is an introduction only, I will try to post about Gadget API, Gadget Settings etc in the next post.</p>
<p>Happy Coding <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/02/24/developing-simple-windows-7-gadget-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>File Uploader using Silverlight and WCF</title>
		<link>http://www.dotnetthoughts.net/2010/01/06/file-uploader-using-silverlight-and-wcf/</link>
		<comments>http://www.dotnetthoughts.net/2010/01/06/file-uploader-using-silverlight-and-wcf/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 02:38:02 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[FileUpload]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=693</guid>
		<description><![CDATA[From Silverlight 2 onwards it supports Open File Dialog, which helps developers to implement upload file logic, with the help of WCF services. Silverlight Open File Dialog offers better control over the existing HTML FileUpload control, like File Filter, File Sizes etc, and it can be managed from client side. Here is a simple implementation [...]]]></description>
			<content:encoded><![CDATA[<p>From Silverlight 2 onwards it supports Open File Dialog, which helps developers to implement upload file logic, with the help of WCF services. Silverlight Open File Dialog offers better control over the existing HTML FileUpload control, like File Filter, File Sizes etc, and it can be managed from client side. Here is a simple implementation of Silverlight uploader with WCF.</p>
<p>Silverlight UI &#8211; XAML</p>
<pre class="brush: xml;">
&lt;Canvas x:Name=&quot;LayoutRoot&quot;&gt;
    &lt;StackPanel Margin=&quot;10&quot; Background=&quot;White&quot; Orientation=&quot;Horizontal&quot;&gt;
        &lt;TextBlock Text=&quot;Select File :&quot; /&gt;
        &lt;TextBox Name=&quot;txtFileName&quot; Width=&quot;200&quot; /&gt;
        &lt;Button Name=&quot;cmdBrowse&quot; Content=&quot;Browse&quot; Click=&quot;cmdBrowse_Click&quot; /&gt;
        &lt;Button Name=&quot;cmdUpload&quot; Content=&quot;Upload&quot; Click=&quot;cmdUpload_Click&quot; /&gt;
    &lt;/StackPanel&gt;
&lt;/Canvas&gt;
</pre>
<p>And the code behind</p>
<pre class="brush: csharp;">
FileInfo fi;
private void cmdBrowse_Click(object sender, RoutedEventArgs e)
{
    //Open File Dialog
    OpenFileDialog dlg = new OpenFileDialog();
    //The _FilterText variable used to control file extentions
    //supported by the Upload control
    dlg.Filter = this._FilterText;
    bool? result = dlg.ShowDialog();
    if (result.HasValue &amp;&amp; result.Value)
    {
        this.fi = dlg.File;
        //The _FileSize variable used to control the
        //Maximum Size supported by the control.
        if (this.fi.Length &gt; this._FileSize)
        {
            //Control will fire a FileSizeError event, if the
            //Uploading File Size greater than the specified.
            if (this._onFileSizeError != null)
            {
                this._onFileSizeError(this, EventArgs.Empty);
            }
            return;
        }
        this.txtFileName.Text = this.fi.Name;
    }
}

private void cmdUpload_Click(object sender, RoutedEventArgs e)
{
    //Uploading part.
    if (this.fi != null)
    {
        byte[] buffer = new byte[this.fi.Length];
        using (Stream s = this.fi.OpenRead())
        {
            //Reading the File Content to the Stream.
            //It is using a WCF Service with Upload File method
            s.Read(buffer, 0, buffer.Length);
            ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            client.UploadFileCompleted += new EventHandler&lt;System.ComponentModel.AsyncCompletedEventArgs&gt;(client_UploadFileCompleted);
            client.UploadFileAsync(this.fi.Name, buffer);
        }
    }
}

private void client_UploadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    //Application will raise another Event
    //after the completion of the File Upload.
    if (this._onFileUploadCompleted != null)
    {
        this._onFileUploadCompleted(this, EventArgs.Empty);
    }
}
</pre>
<p>And here is the WCF Service with Upload File method.</p>
<pre class="brush: csharp;">
//IService1 Interface
[ServiceContract]
public interface IService1
{
    [OperationContract]
    void UploadFile(string fileName, byte[] content);
}

//IService1 Interface implementation
public class Service1 : IService1
{
    public void UploadFile(string fileName, byte[] content)
    {
        using (FileStream sw = File.OpenWrite(Path.Combine(@&quot;C:\Uploads&quot;, fileName)))
        {
            sw.Write(content, 0, content.Length);
        }
    }
}
</pre>
<p>To enable communication between Silverlight and Javascript the &#8220;System.Windows.Browser&#8221; namespace is used. Which supports various attributes and methods, will expose the properties and events of the silverlight application to the Javascript. </p>
<pre class="brush: csharp;">
public MainPage()
{
    InitializeComponent();
    //This method enables the scripting support to the Application.
    //From Javascript developer can access the Properties and events
    //like SilverlightObj.Content.Page.Property
    HtmlPage.RegisterScriptableObject(&quot;Page&quot;, this);
}

private string _FilterText = &quot;Text Files|*.txt&quot;;
private int _FileSize = 1000;
private event EventHandler _onFileSizeError;
private event EventHandler _onFileUploadCompleted;

//This attribute used to expose the Property to Client side.
//From Javascript : ctrl.Content.Page.FileSize = 2000;
[ScriptableMember]
public int FileSize
{
    get
    {
        return this._FileSize;
    }
    set
    {
        this._FileSize = value;
    }
}
[ScriptableMember]
public string Filter
{
    get
    {
        return this._FilterText;
    }
    set
    {
        this._FilterText = value;
    }
}

[ScriptableMemberAttribute]
public EventHandler OnFileSizeError
{
    get
    {
        return this._onFileSizeError;
    }
    set
    {
        this._onFileSizeError = value;
    }
}

[ScriptableMemberAttribute]
public EventHandler OnFileUploadCompleted
{
    get
    {
        return this._onFileUploadCompleted;
    }
    set
    {
        this._onFileUploadCompleted = value;
    }
}
</pre>
<p>And in the Javascript, the property can access and can modify it.</p>
<pre class="brush: jscript;">
function onPluginLoaded() {
    var ctrl = document.getElementById(&quot;silverlightControlHost1&quot;);
    ctrl.Content.Page.Filter = &quot;Image Files|*.jpg;*.gif;*.png|All Files(*.*)|*.*&quot;;
    ctrl.Content.Page.FileSize = 20000;
    ctrl.Content.Page.OnFileSizeError = function () {
        alert(&quot;File Size should be less than 2 KB&quot;);
    };
    ctrl.Content.Page.OnFileUploadCompleted = function () {
        alert(&quot;Uploaded successfully !&quot;);
    };
}
</pre>
<p>And the HTML Part &#8211; Here the onLoad parameter will assign the Javascript to execute on loading of the silverlight application.</p>
<pre class="brush: xml;">
&lt;object id=&quot;silverlightControlHost1&quot; data=&quot;data:application/x-silverlight-2,&quot; type=&quot;application/x-silverlight-2&quot;
    width=&quot;100%&quot; height=&quot;100%&quot;&gt;
    &lt;param name=&quot;onLoad&quot; value=&quot;onPluginLoaded&quot; /&gt;
&lt;/object&gt;
</pre>
<p>And here is the screen shot, of the same running on my Windows 7 machine.</p>
<div id="attachment_695" class="wp-caption alignnone" style="width: 310px"><a href="http://www.dotnetthoughts.net/wp-content/uploads/2010/01/application.png"><img src="http://www.dotnetthoughts.net/wp-content/uploads/2010/01/application-300x238.png" alt="Silverlight File Uploader" title="Silverlight File Uploader" class="size-medium wp-image-695" /></a><p class="wp-caption-text">Silverlight File Uploader</p></div>
<p>Please let me know your comments and feedbacks. Happy Programming. <img src='http://www.dotnetthoughts.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2010/01/06/file-uploader-using-silverlight-and-wcf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
