Skip to main content
Home Forums Silverlight Programming Programming with .NET - General How to pass the parameters in silverlight control to aspx pages
8 replies. Latest Post by SteveReconG on March 16, 2009.
(1)
luojing
Member
9 points
27 Posts
03-27-2008 9:58 PM |
Hi,I want to get the data in xaml from aspx pages,like this
page.xaml:
<WatermarkedTextBox x:Name="UserName" />
<WatermarkedTextBox x:Name="Password" />
Default.aspx:
what can i do to get the UserName.text and Password.text in Default.aspx or Default.aspx.cs?
thanks in advance!
mchlsync
Star
14566 points
2,730 Posts
03-27-2008 10:05 PM |
#1. You can invoke this page by using HttpWebRequest or XmlHttpRequest Wrapper. Add those values as a parameters in POST request.
OR
#2. You can use cookie. You can create a cookie in Silverlight and read that cookies from ASP.NET.
03-27-2008 11:39 PM |
mchlSync: #1. You can invoke this page by using HttpWebRequest or XmlHttpRequest Wrapper. Add those values as a parameters in POST request. OR #2. You can use cookie. You can create a cookie in Silverlight and read that cookies from ASP.NET.
hi,michael
i can't find HttpWebRequest or XmlHttpRequest in my project,need i add system.net namespace?could you show me example to use HttpWebRequest or XmlHttpRequest ?
thanks
03-28-2008 12:04 AM |
luojing:i can't find HttpWebRequest or XmlHttpRequest in my project,need i add system.net namespace?
Yes. you have to add System.Net.dll as a ref. declare using System.Net namespace.
luojing:could you show me example to use HttpWebRequest or XmlHttpRequest ?
Note: Those are just sample so that I can't promise you that this is the exact code that you can directly use. So, please remember you have to change based on your requirement.
HttpWebRequest (Ref: http://msdn2.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx)
private void InvokeAsync() { string serviceURL = "http://localhost:52976/SL2Test/default.aspx"; pMessage = "test"; // Create a new HttpWebRequest object. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceURL); //// Set the ContentType property. ////equest.ContentType = "application/json"; // Set the Method property to 'POST' to post data to the URI. request.Method = "POST"; // Start the asynchronous operation. request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request); // Keep the main thread from continuing while the asynchronous // operation completes. A real world application // could do something useful such as updating its user interface. allDone.WaitOne(); // Get the response. request.BeginGetResponse(new AsyncCallback(ResponseCallback), request); } private static void ResponseCallback(IAsyncResult asynchronousResult) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; HttpWebResponse resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult); Stream streamResponse = resp.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); string responseString = streamRead.ReadToEnd(); Console.WriteLine(responseString); // Close the stream object. streamResponse.Close(); streamRead.Close(); // Release the HttpWebResponse. resp.Close(); } private static void ReadCallback(IAsyncResult asynchronousResult) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; // End the operation. Stream postStream = request.EndGetRequestStream(asynchronousResult); string postData = pMessage; // Convert the string into a byte array. byte[] byteArray = Encoding.UTF8.GetBytes(postData); // Write to the request stream. postStream.Write(byteArray, 0, postData.Length); postStream.Close(); allDone.Set(); }
XmlHttpRequest Wrapper
If you know Ajax then you already know about how to use it. Please take a look http://silverlight.net/forums/p/11508/36876.aspx or this post.
Justin-J...
380 points
114 Posts
03-28-2008 6:27 AM |
Hi loujing,
Excellent question. This really opens up the door to various issues.
So basically you'd like to reference your Silverlight XAML TextBox's (and other UIElements) the same you would have your ASP.Net server controls?
The first limitation is understanding that ASP.Net is server-side and Silverlight (like Javascript) is client-side. The reason i've mentioned Javascript is that there're already a set of Best practices in place for collaboration between ASP.Net & Javascript which would apply to Silverlight.
The final concept should be "ASP.Net writes Javascript/HTML which is accessible to Silverlight. Silverlight writes hidden Html fields which ASP.Net reads on postback".
The most basic implementation of this can be having ASP.Net write out a Javascript array (with ScriptManager.RegisterArrayDeclaration), send it to the Silverlight application as InitParams, and have silverlight read them and take the appropriate actions (http://nerddawg.blogspot.com/2008/03/how-to-pass-initialization-params-to.html).
For instance, you could send an array with keys of the "ElementName.ElementProperty" and the array value would be the PropertyValue.
Then if you'd like Silverlight to send these values back to ASP.Net you'd have to build a Viewstate like HTML hidden field silverlight writes too and ASP.Net reads from.
hope this helps.
03-28-2008 1:30 PM |
Justin-Josef Angel [MVP]:The most basic implementation of this can be having ASP.Net write out a Javascript array (with ScriptManager.RegisterArrayDeclaration), send it to the Silverlight application as InitParams, and have silverlight read them and take the appropriate actions
hi,Justin:
thank you give me a very useful method to pass parameters between Silverlight and ASP.Net,this method is more effective and basic,could you show me more Details,or demo,that's better! thanks a lot.
03-29-2008 1:53 AM |
Actually, it's about adding hidden file or hidden HTML element (e.g. textbox with display:none or etc) in your aspx or html page where you host the SL. then you can use the managed HTML bright to update or ead this hidden element from Silverlight. You can also do the same from ASP.NET.
For example >>
Silveright >> Hidden Field >> ASP.NET
Silveright << Hidden Field << ASP.NET
laurent_31
4 points
2 Posts
07-03-2008 9:17 AM |
Hello,
Can you give more details about comunication with cookies? I try to do that but I need help.
In my Silverlight 2 application :
expires =
When I do that, my cookies is create and I can see it. But after I can't get it with my ASP.NET page :
Can you help me?
Thanks
SteveReconG
18 points
26 Posts
03-16-2009 12:12 PM |
What about passing hidden field values and using html bridge?