Select to view content in your preferred language

ESRI silverlight control on .NET page?

774
2
10-24-2011 08:58 AM
EvanBossett
Emerging Contributor
can i have an ESRI Mapping Silverlight control on a web page in my Visual Studio 2010 web application project that interacts with the page?  i.e. if the user selects an item from a list on the aspx page, can I send that input to the silverlight map and perform an action?

is this good coding practice? 

thank you
0 Kudos
2 Replies
TerryGiles
Frequent Contributor
Might not be exactly what you're looking for but you can call methods in the Silverlight classes from JavaScript, if you flag the class with the ScriptableType() attribute & flag the method(s) you want exposed to JavaScript with the ScriptableMethod() attribute.  Silverlight objects can also call JS functions in your webpage via the ScriptObject class.

calling JS function from within SL-
// in SL code behind -
            ScriptObject js = (ScriptObject)HtmlPage.Window.GetProperty("some_js_function");
            js.InvokeSelf("called from SL");
//Javascript in page -
            function some_js_function(arg) {
                alert(arg.toString());
            }



Call SL method from JavaScript
// JS function in the page, called by a button's onclick
            function Call_SL_Method(arg) {
                var sl = document.getElementById("silverlightControl");
                sl.content.Page.SetUserName(arg);
            }

//in Silverlight Code Behind, decorate the class & method to expose to JS
    [ScriptableType()]
    public partial class MainPage : UserControl
    { 

        [ScriptableMember()]
        public void SetUserName(string name)
        { 
            //txtName is a textblock to hold user name
            txtName.Text = name;
        }
        public MainPage()
        {
            InitializeComponent();

            //register the class with the HtmlPage 
            HtmlPage.RegisterScriptableObject("Page", this);

            ....
         }

0 Kudos
EvanBossett
Emerging Contributor
Thank you very much. This appears to be exactly what I was wondering about. 

I just wasn't sure if this was possible or advisable because none<?> of the ESRI examples or sample code show the component being used this way.

If i am wrong and their are examples which I have somehow missed, I'd appreciate the URLs.

Thanks again - either way, this should be enough to work from.
0 Kudos