Quantcast
Channel: Beyond web Logs
Viewing all articles
Browse latest Browse all 90

How to submit or POST form data using Silverlight in ASP.NET ?

$
0
0

As you probably already know, Silverlight is a browser plugin for providing rich web content. It includes a subset of the capabilities of WPF, and it aims to rival Adobe Flash. However, submitting form data using POST in silverlight is not as trivial as you might imagine. You would need to create HttpWebRequest method to POST your silverlight based form data and that’s not all. You will also need to go through many pitfalls like Synchronization Context issues.

Here is the sample code to submit or POST form data in ASP.NET using Silverlight. I hope it would help you a lot in intergating your silverlight application in ASP.NET.

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
syncContext = SynchronizationContext.Current
For Each uie As UIElement In MasterPage.Children
GetChilds(uie)
Next
Dim url As New Uri(HtmlPage.Document.DocumentUri, “processfile.aspx”)
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
request.Method = “POST”
request.ContentType = “application/x-www-form-urlencoded”
request.BeginGetRequestStream(New AsyncCallback(AddressOf RequestProceed), request)
End Sub

Private Sub RequestProceed(ByVal asyncResult As IAsyncResult)
Dim request As HttpWebRequest = DirectCast(asyncResult.AsyncState, HttpWebRequest)
Dim postData As New StreamWriter(request.EndGetRequestStream(asyncResult))
postData.Write(formData)
postData.Close()
request.BeginGetResponse(New AsyncCallback(AddressOf RProceed), request)
End Sub

Private Sub RProceed(ByVal asyncResult As IAsyncResult)
Dim request As HttpWebRequest = DirectCast(asyncResult.AsyncState, HttpWebRequest)
Dim response As HttpWebResponse = request.EndGetResponse(asyncResult)
syncContext.Post(AddressOf ExtractResponse, response)
End Sub

Private Sub ExtractResponse(ByVal state As Object)
Dim response As HttpWebResponse = TryCast(state, HttpWebResponse)
Dim url As New Uri(HtmlPage.Document.DocumentUri, “YourPage.aspx”)
HtmlPage.Window.Eval(“window.location.href=’” & url.ToString & “‘;”)
End Sub

Private Sub GetChilds(ByVal uie As UIElement)
Dim obj As String
obj = uie.ToString
names = names & obj

If obj = “System.Windows.Controls.StackPanel” Then
Dim sp As StackPanel
sp = DirectCast(uie, System.Windows.Controls.StackPanel)
For Each spChild As UIElement In sp.Children
GetChilds(spChild)
Next
End If

If obj = “System.Windows.Controls.Grid” Then
Dim gv As Grid
gv = DirectCast(uie, System.Windows.Controls.Grid)
For Each gvChild As UIElement In gv.Children
GetChilds(gvChild)
Next
End If
If obj = “System.Windows.Controls.TextBox” Then
Dim tb As TextBox
tb = DirectCast(uie, System.Windows.Controls.TextBox)
formData = formData & tb.Name & “$#*”
formData = formData & tb.Text & “$#*;”
End If
If obj.Contains(“System.Windows.Controls.RadioButton”) Then
Dim rb As RadioButton
rb = DirectCast(uie, System.Windows.Controls.RadioButton)
formData = formData & rb.Name & “$#*”
formData = formData & rb.IsChecked & “$#*;”
End If
If obj.Contains(“System.Windows.Controls.CheckBox”) Then
Dim cb As CheckBox
cb = DirectCast(uie, System.Windows.Controls.CheckBox)
formData = formData & cb.Name & “$#*”
formData = formData & cb.IsChecked & “$#*;”
End If
End Sub

Hope this helps!


Viewing all articles
Browse latest Browse all 90

Trending Articles