Silverlight: Passing null values or empty strings (revisited)

(wrote this in the plane from JFK to Las Vegas)

In a previous post, I mentioned that it's not possible to pass empty strings from JavaScript to a Silverlight application. It seems that it was not correct, or at least not complete:

It is in fact possible to pass null values or empty strings, but not the content of an empty HTML textfield (input type="text"). Let's see:

<body> <div id="SilverlightControlHost" class="silverlightHost" > <script type="text/javascript"> createSilverlight(); </script> </div> <div id="HtmlForm" style="position: absolute; top: 50px; left: 50px; background-color: Green; padding: 10px;"> <form action="TestPage.html"> <input type="text" id="InputTextBox1" /> <br /> <input type="button" value="Go" onclick="callDotNet();" /> </form> </div> </body>
HTML code
function callDotNet() { var string1 = document.getElementById("InputTextBox1").value.toString(); slControl.content.Page.Go1(string1); slControl.content.Page.Go2(null); slControl.content.Page.Go3(""); slControl.content.Page.Go4("Test"); }
JavaScript code
[Scriptable] public partial class Page : Canvas { public void Page_Loaded(object o, EventArgs e) { // Required to initialize variables InitializeComponent(); WebApplication.Current.RegisterScriptableObject("Page", this); } [Scriptable] public void Go1(string message1) { DisplayTextBlock1.Text = (message1 == null) ? "null" : ((message1.Length == 0) ? "empty" : message1); } [Scriptable] public void Go2(string message1) { DisplayTextBlock2.Text = (message1 == null) ? "null" : ((message1.Length == 0) ? "empty" : message1); } [Scriptable] public void Go3(string message1) { DisplayTextBlock3.Text = (message1 == null) ? "null" : ((message1.Length == 0) ? "empty" : message1); } [Scriptable] public void Go4(string message1) { DisplayTextBlock4.Text = (message1 == null) ? "null" : ((message1.Length == 0) ? "empty" : message1); } }

Running the example shows that empty string and null values are passed just fine. However, if the HTML text field is empty, the method "Go1" is never even called. So as a consequence, always remember to check the value of a HTML text field before calling a Silverlight method with that value.

Note that we are going to get a new CTP of Silverlight this week, so it'll be interesting to check if that's still the case in this new version.

Print | posted on Monday, March 03, 2008 2:15 PM

Feedback

# re: Silverlight: Passing null values or empty strings (revisited)

left by shashi at 11/16/2009 6:04 PM Gravatar
Very Nice
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: