Link to home
Start Free TrialLog in
Avatar of MichaelDavidCarr
MichaelDavidCarr

asked on

How to pass javascript variables back to c#

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

ASKER CERTIFIED SOLUTION
Avatar of Md Shah
Md Shah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MichaelDavidCarr
MichaelDavidCarr

ASKER

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.