Skip to main content

Microsoft Silverlight

Answered Question How to pass data using scriptableMember method RSS Feed

(0)

chandi
chandi

Member

Member

3 points

22 Posts

How to pass data using scriptableMember method

Hi, I have asp.net web application and silverlight project.When I click a button in asp.net .aspx page it goes to silverlight project and there loaded event call to javascript. In that javascript call a scriptableMember method in silverlight project. The reason is to use scriptableMember method in silverlight project is to pass data from asp.net application to silverlight project. My problem is how can I pass values to scriptableMember method parameters from asp.net web application. Thanx

davidezordan
davidezo...

Contributor

Contributor

5686 points

873 Posts

Silverlight MVP

chandi
chandi

Member

Member

3 points

22 Posts

Re: How to pass data using scriptableMember method

Hi david,

Thanx for your reply.

I developed my application as second article.

Page.xaml

namespace SilverlightApplication
{
   public partial class Page : UserControl   
   {       
        public Page()
        {           
              InitializeComponent();
              HtmlPage.RegisterScriptableObject("Page", this);                   
              HtmlPage.Window.Invoke("TalkToJavaScript", "Hellofrom Silverlight");
        }     

        [ScriptableMember]       
        public void UpdateText(string result)       
        {           
               myTextbox.Text = result;       
        }   
   }
}

<script type="text/javascript">        
 function TalkToJavaScript( data){           
        alert("Message received from Silverlight: "+data); 
         var control = document.getElementById("silverlightControl");                               

         control.Content.Page.UpdateText("Hello from Javascript!");        
  }   
</script>

Here UpdateText ScriptableMember method parameter value has hardcoded.

I need to pass my entered TextBox string to UpdateText method.

How can I do that.

Thanx

davidezordan
davidezo...

Contributor

Contributor

5686 points

873 Posts

Silverlight MVP
Answered Question

Re: Re: How to pass data using scriptableMember method

You can use another [ScriptableMember]:

public MainPage()

{

InitializeComponent();

HtmlPage.RegisterScriptableObject("Page", this); HtmlPage.Window.Invoke("TalkToJavaScript", "Hellofrom Silverlight");

}

[ScriptableMember]

public void UpdateText(string result)

{

myTextBox.Text = result;

}

[
ScriptableMember]public string GetText()

{

return myTextBox.Text;

}

<script type="text/javascript">

function TalkToJavaScript(data) {

alert("Message received from Silverlight: " + data);

var control = document.getElementById("silverlightControl");

control.Content.Page.UpdateText(control.Content.Page.GetText());

}

</script>

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

chandi
chandi

Member

Member

3 points

22 Posts

Re: Re: How to pass data using scriptableMember method

Hi,

Thank you very much for your reply.

In your example myTextBox is a xaml page control.

Actually I need a call method in asp.net according to entered value.

1. In my asp.net page there is a button.

2. When button clicked it pops up a child window which is in silverlight project.

3. In my child window there is a two combo box fields.

4. According to selected item in first combo, second combo should be populate.

5. This data populated method couldn't write in xaml.cs page as assembly reference not supported. Therefor it is in asp.net.so when I selected an item from first combo it goes to javascript function and from there should call a method.

How can I do that.

Please help me.

Thanx          

 

Min-Hong Tang - MSFT
Min-Hong...

Contributor

Contributor

3376 points

377 Posts

Answered Question

Re: Re: How to pass data using scriptableMember method

Hi,

   Em, you mean after you selected something in your first comboBox then your code-behind should pass value to your javascript and then process it and generate some results and at last pass the result back to your second comboBox which is a silverlight control.  If i didn't get you wrong. Then all you need is anther scriptablemember which can accept some value.

In your codeBehind:   

     [ScriptableMember]
        public void SetText(object e) {
           //  do your stuff

        }

  Then in your asp.net page javascript you can just invoke it alike invoking the GetText() member.  About how to dynamically add a control you can check  links below.

http://forums.silverlight.net/forums/t/9298.aspx

http://forums.silverlight.net/forums/t/135977.aspx

Best Regards

Min-Hong Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Min-Hong Tang - MSFT
Min-Hong...

Contributor

Contributor

3376 points

377 Posts

Answered Question

Re: How to pass data using scriptableMember method

Hi,

    By the way, there are other ways to pass values from asp.net to SL application. Em, say to use InitParams or to use cookies.

    http://msdn.microsoft.com/en-us/library/dd920298(VS.95).aspx  This link show how to get and set cookies in silverlight application.

    http://msdn.microsoft.com/en-us/library/cc838255(VS.95).aspx This link show how to use InitParams.

    And hope you find the best solution as soon as possible.

Best Regards

Min-Hong Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities