Avatar of 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>
        <title>My title</title>
            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?
        <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" />
		<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" />

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

JavaScriptC#.NET Programming

Avatar of undefined
Last Comment

8/22/2022 - Mon
Md Shah

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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.
Your help has saved me hundreds of hours of internet surfing.