Skip to main content

Microsoft Silverlight

Answered Question How to pass the parameters in silverlight control to aspx pagesRSS Feed

(1)

luojing
luojing

Member

Member

9 points

27 Posts

How to pass the parameters in silverlight control to aspx pages

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
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: How to pass the params in silverlight control to aspx pages

#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.  

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


luojing
luojing

Member

Member

9 points

27 Posts

Re: How to pass the params in silverlight control to aspx pages

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

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP
Answered Question

Re: How to pass the params in silverlight control to aspx pages

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.

 

 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


Justin-Josef Angel [MVP]
Justin-J...

Member

Member

380 points

114 Posts

Answered Question

Re: How to pass the parameters in silverlight control to aspx pages

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.  

---
Justin-Josef Angel
Senior .Net consualtent,
Microsoft Most Valueable Proffesional

Website http://www.JustinAngel.Net
Blog http://blogs.Microsoft.co.il/blogs/JustinAngel
Cell (+972) 546-567789
Office (+972) 03-9504364
Email J@JustinAngel.Net

Got Silverlight 1.0 Javascript Intellisense? www.codeplex.com/intellisense
Got Silverlight 1.1 Hebrew & Arabic? www.codeplex.com/SilverlightRTL

luojing
luojing

Member

Member

9 points

27 Posts

Re: How to pass the parameters in silverlight control to aspx pages

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 youYes 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.

mchlsync
mchlsync

Star

Star

14606 points

2,730 Posts

Silverlight MVP

Re: How to pass the parameters in silverlight control to aspx pages

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

 

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


laurent_31
laurent_31

Member

Member

4 points

2 Posts

Re: How to pass the parameters in silverlight control to aspx pages

Hello,

Can you give more details about comunication with cookies? I try to do that but I need help.

In my Silverlight 2 application :

DateTime expires = new DateTime();

expires = DateTime.UtcNow.AddDays(4);

HtmlPage.Document.Cookies = string.Format("{0}={1}; expires={2}, {3} {4} {5} {6}:{7:00}:00 UTC","testResume", myTestXML,expires.DayOfWeek.ToString(),expires.Day, expires.Month, expires.Year,expires.Hour, expires.Minute);

 

 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 :

HttpCookie c;

if (Response.Cookies["testResume"]!=null)

c = Response.Cookies["testResume"];

 

Can you help me?

Thanks

SteveReconG
SteveReconG

Member

Member

18 points

26 Posts

Re: Re: How to pass the parameters in silverlight control to aspx pages

 What about passing hidden field values and using html bridge?

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities