How to pass javascript variables back to c#

MichaelDavidCarr
MichaelDavidCarr used Ask the Experts™
on
First, thank you for your time.

I am building a c# Silverlight Lightswitch application.  If you are not that familiar with LightSwitch then think of this as a regular ole silverlight aspx .net application.  

In the first window I have a WebBrowser control that will show the html code below.  Basically, all this html does is present the user with a menu of tutorial videos.  The user then clicks on a tutorial image.  The javascript code captures the id and href of the clicked on tutorial and should pass them to the c# code.

The c# code will play the video in the second window.

Passing the clicked on id and href of the clicked on tutorial to the c# code is where I am stuck.  

I am not emotionally wedded to a particular approach I just want the thing to work.  You can tell me how to do this with Data Binding, Script Manager or even some other approach and I would be happy with any solution.  By the way, Silverlight might get in the way of the approach suggested.  If it does, we will need to explore another approach.

Thank you very much.

<!DOCTYPE html>
<html>
    <head>
        <title>My title</title>
        <script>
            function playTutorial(id, href) {
				// A thumbnail/image was clicked on.  
				// Attempt 1) Place the incoming parameters into fields that can be referenced by the code behind.
                document.getElementById("ClickedOnIdHidden").value = id;
                document.getElementById("ClickedOnHrefHidden").value = href;

				// Attempt 2) Place the incoming parameters into fields that can be referenced by the code behind.
				document.getElementById("<%= ClickedOnIdHidden%>").value = id;
				document.getElementById("<%= ClickedOnHrefHidden%>").value = href;

				// The following PlayTutorialN methods will show and play the href in another window 
				// along with other housekeeping chores.
			
				// Attempt 1) Call the C# public method PlayTutorial1 and that method 
				// will reference the hidden fields to get the values.
                window.external.PlayTutorial1();			// PlayTutorial1 is not found!
				
				// Attempt 2) Call the C# public method PlayTutorial2 and pass the values in.
                window.external.PlayTutorial2(id, href);	// PlayTutorial2 is not found!
				
				// Attempt 3) Raise some event (like a Click event or a Notify event) that is
				// captured on the C# side.  That click event handler will get the values.
				// What code goes here?
            }
        </script>
    </head>
    <body>
        <input id="ClickedOnIdHidden" runat="server" />    <!-- During debugging remove the hidden attribute:  type="hidden" -->
        <input id="ClickedOnHrefHidden" runat="server" />  <!-- During debugging remove the hidden attribute:  type="hidden" -->

		<div class="ThumbnailSection" onclick="playTutorial(1, 'https://www.youtube.com/watch?v=0t8yDnyOaQ8')">
			<img src="http://www.socialmobilemarketingsystems.com/MasterSchedule/DummyThumbnail.png" alt="A tutorial" />
		</div>
		<p />
		<div class="ThumbnailSection" onclick="playTutorial(2, 'https://www.youtube.com/watch?v=RKyGDWeblQw')">
			<img src="http://www.socialmobilemarketingsystems.com/MasterSchedule/DummyThumbnail.png" alt="A tutorial" />
		</div>
    </body>
</html>

Open in new window


public partial class Tutorials
{
	partial void Tutorials_Created()
	{
	}
	
        // This method should be called by the javascript code when the user clicks
        // on the tutorial image.
	public void PlayTutorial(int tutorialId, string tutorialHref)
	{
		// This method will play the tutorial.
                // /Even though the code is not here, I know how to do this part.
                // 
	}
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014
Commented:
Dear Michael,

I am not an expert in Silverlight.

However, as you have said "I would be happy with any solution" in your post, I have created a sample project for you which will meet your expectations.

Download the code from below:

https://drive.google.com/file/d/0B69KXMXvsWi_WFE1VU1YbDQ0UWc/edit?usp=sharing

Hope this resolves your issue :)

Author

Commented:
MD Shah went above and beyond the call of duty in creating an example application for me.  It didn't solve the problem because, as I initially suspected, Silverlight might get in the way and it did with MD Shah's suggestion.  I did some more research and I found out that in order to solve my problem, I first need to elevate my application to full trust.  This might require another conversation with the experts.

Thank you again MD Shah.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial