简体   繁体   English

如何在C#的同一页面上使ASP.NET Control与Silverlight应用程序对话

[英]How do you make a ASP.NET Control talk to a Silverlight app on the same page in C#

For example: 例如:

First, say I have a Silverlight app with Windowless=true so that I can place ASP.NET controls on top of it. 首先,假设我有一个Silverlight应用程序,其Windowless = true,以便可以将ASP.NET控件放在其顶部。 Then I place an ASP.NET button on the page. 然后,在页面上放置一个ASP.NET按钮。 How can I have say the text of a control in the Silverlight app change when the user presses the ASP.NET button? 当用户按下ASP.NET按钮时,如何说出Silverlight应用程序中控件的文本发生变化? How would I send the Silverlight app an update message from the C# code that catches the Click of the ASP.NET button? 我如何从C#代码向Silverlight应用发送更新消息,以捕获ASP.NET按钮的单击?

Thanks, Jeff 谢谢杰夫

From what I can tell, based on your question, the Silverlight application is running in a web browser and you have it embedded in an asp.net page? 据我所知,根据您的问题,Silverlight应用程序正在Web浏览器中运行,并且已将其嵌入到asp.net页面中? The code for the asp.net button that you are dragging on the page lives on the server and gets sent to the web browser as html. 您在页面上拖动的asp.net按钮的代码位于服务器上,并以html格式发送到Web浏览器。 When you click the button on the page it is submitting form data back to the server which ASP.NET interprets and calls your button click code. 当您单击页面上的按钮时,它会将表单数据提交回由ASP.NET解释并调用您的按钮单击代码的服务器。 Since that code is executing on the server it cannot get to the silverlight app. 由于该代码正在服务器上执行,因此无法访问Silverlight应用程序。 If you really need to interact with the silverlight application directly on the client you would use javascript in the browser. 如果确实需要直接在客户端上与silverlight应用程序进行交互,则可以在浏览器中使用javascript。

Here is a basic example: http://blogs.vertigo.com/personal/ralph/Blog/archive/2008/05/15/call-silverlight-from-javascript-call-javascript-from-silverlight.aspx 这是一个基本示例: http : //blogs.vertigo.com/personal/ralph/Blog/archive/2008/05/15/call-silverlight-from-javascript-call-javascript-from-silverlight.aspx

From memory, you need to expose a scriptable member from your silverlight application, eg: 从内存中,您需要从Silverlight应用程序中公开可编写脚本的成员,例如:

[ScriptableMember()]
public void ChangeText(string newText)
{
    // Update your text control here
}

and register it for scripting from javascript in the constructor: 并在构造器中将其注册以从javascript编写脚本:

public MySilverlight()
{
    InitializeComponent();
    HtmlPage.RegisterScriptableObject("MyObject", this);
}

You can then invoke it from javascript as; 然后您可以从javascript中调用它;

function ChangeText()
{
    var yourObject = getElementById("yourObjectID");
    yourObject.Content.MyObject.ChangeText("New Text");
}

Then just wire up the button's client click to invoke the javascript ChangeText method. 然后只需连接按钮的客户端单击以调用javascript ChangeText方法。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ASP.NET 内核制作 Razor 页面和 C# 代码框架? - How to do you make both a Razor Page and C# code framework with ASP.NET Core? 如何从Silverlight应用程序内重定向包含Silverlight应用程序的asp.net页面? - How do I redirect the asp.net page that contains a silverlight app from within the silverlight app? 使用Silverlight创建自定义控件C#ASP.NET - Using Silverlight to create a custom control C# ASP.NET 如何在ASP.NET C#中使用if(IsPostBack) - How do you use if (IsPostBack) in ASP.NET C# 如何通过C#更新同一asp.net页中的标签 - How to update labels in the same asp.net page by C# 使用C#在asp.net中的其他页面中控制标签 - Control label in other page in asp.net using C# 如何使用 ASP.NET Core / Razor 通过单击按钮运行在 Razor 页面上编写的 C#? - How do you run C# written on a Razor page from a button click with ASP.NET Core / Razor? ASP.NET C#-是否需要为同一数据库中不同表的每个gridview提供单独的数据源? - ASP.NET C# - Do you need seperate datasources for each gridview of different tables in same database? Asp.net 日历控件:如何将事件添加到数据库? - Asp.net calendar control: how do you make events to Database? 您可以在ASP.net文本框中创建“分隔符”吗? (C#) - Can you make a “separator” in ASP.net Textboxes? (C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM