May 29

Submiting a form from a silverlight page

Posted in Silverlight      Comments Off on Submiting a form from a silverlight page

If you”re using Silverlight 1.1 alpha, you might need to submit a form from your silvelright page”s codebehind file. You can do this easily by calling the static method Submit exposed by the HtmlPage class. It”s as simple as doing this:


There”s also an override which lets you specify the name of the form you want to submit. Unfortunately, if you”re integrating Silverlight on an ASP.NET page, you might face some problems which you might not expect. For starterts, you”ll get an empty __EVENTTARGET value in your postback. Your first attempt might be to handle the onsubmit event from javascript and fill the value of that hidden field. Unfortunately, I believe that the Silverlight control will submit your page by calling the IHTMLFormElement::submit method (or something along those lines). This mean that you won”t get notified during the submission of the form (in other words, the onsubmit event won”t be fired).

A possible workaround for this is to set the __EVENTTARGET hidden field from your codebehind page. Something like this will work without any problems:

HtmlElement eventTarget= HtmlPage.Document.GetElementByID(“__EVENTTARGET”);
eventTarget.SetAttribute(“value”, “bt”);

As you might guess, this isn”t really production code. In a real app, you”d probably need to send more info or do more things before submitting the form. In those cases, you might, for instance,expose an event which would be handled by Javascript code. You would at least want to receive the ID of the button,which you could do by exposing a scriptable property.