How can I return data to c# from javascript?

Jan 18, 2011 at 11:56 AM

Hi,

I have completed the Hello World example and moved onto a simple example of my own - user entry validation.  I have the javascript checking the user's entry and displaying a message to the console dependant upon the user's entry and this works ok.

What I want to do next is to run c# code dependant upon the user's entry.  This is a what I have (this method is called after the user's entry has been captured):

static void ValidateUserName(string username_c)
{
JavascriptContext validateName = new JavascriptContext();
validateName.SetParameter("console", new SystemConsole()); validateName.SetParameter("username_js", username_c);
validateName.Run("if(username_js.length==0){console.Print('Please enter your name');}");
}

Instead what I want is something kind of like this:

 static bool ValidateUserName(string username_c)
{
bool status = true;
JavascriptContext validateName = new JavascriptContext();
validateName.SetParameter("console", new SystemConsole()); validateName.SetParameter("username_js", username_c); validateName.Run("if(username_js.length==0){ '" + status=false + "'} else{'" + status=true + "'}");         
return status; 
}

Clearly you can't just break out into c# midway through the javascript Run call but hopefully you can see what I'm trying to achieve.

Many thanks
Simon

Coordinator
Jan 18, 2011 at 5:12 PM
Edited Jan 18, 2011 at 5:12 PM

Please refer to the Quick integration sample.

You will notice a "number" variable is assigned in the Javascript scope and then used in the C# scope. This is provided as an example on how to retrieve script execution result.

You could register your status variable in the context, assign a value to your variable based on conditions in the script, and then use GetParameter( ) to use the result in the C# code.

We hope this helps.

 

Jan 19, 2011 at 2:39 AM

Ahh... I'd already done it then when I followed the example before.

Ok, I see it now, thanks.