C# 从外部类访问html控件
为了在C# 从外部类访问html控件,c#,asp.net,wcf,C#,Asp.net,Wcf,为了在ASP.NET网站和C#应用程序之间实现通信通道,我遵循以下示例: 在我的html中有一个ASP.NET标签: <asp:Label runat="server" id="label"></asp:Label> 下面是页面后面的代码: using Interfaces; using System; using System.ServiceModel; using System.Web.Services; using System.Web.UI; publi
ASP.NET网站
和C#应用程序
之间实现通信通道,我遵循以下示例:
在我的html中有一个ASP.NET标签
:
<asp:Label runat="server" id="label"></asp:Label>
下面是页面后面的代码:
using Interfaces;
using System;
using System.ServiceModel;
using System.Web.Services;
using System.Web.UI;
public partial class _Default : Page
{
static IService service;
protected void Page_Load(object sender, EventArgs e)
{
var callback = new Callback();
var context = new InstanceContext(callback);
var pipeFactory = new DuplexChannelFactory<IService>(context, new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/ipc"));
try
{
service = pipeFactory.CreateChannel();
service.Connect();
System.Diagnostics.Debug.WriteLine("Server connected!");
}
catch (EndpointNotFoundException)
{
System.Diagnostics.Debug.WriteLine("Server not found");
}
}
}
public class Callback : ICallbackService
{
public void SendCallbackMessage(string message)
{
// label.Text = message;
}
}
使用接口;
使用制度;
使用System.ServiceModel;
使用System.Web.Services;
使用System.Web.UI;
公共部分类\u默认值:第页
{
静态设备服务;
受保护的无效页面加载(对象发送方、事件参数e)
{
var callback=new callback();
var context=newinstancecontext(回调);
var pipeFactory=new DuplexChannelFactory(上下文,new NetNamedPipeBinding(),new EndpointAddress(“net”)。pipe://localhost/ipc"));
尝试
{
service=pipeFactory.CreateChannel();
service.Connect();
System.Diagnostics.Debug.WriteLine(“服务器已连接!”);
}
捕获(EndpointNotFoundException)
{
System.Diagnostics.Debug.WriteLine(“未找到服务器”);
}
}
}
公共类回调:ICallbackService
{
公共无效SendCallbackMessage(字符串消息)
{
//label.Text=消息;
}
}
当我从另一个应用程序收到消息时,在“Callback”类中,我希望访问网页上的“label”控件
我不知道如何从这个外部类中检索html控件。除了您的页面之外,任何内容都不应该访问这些控件。相反,根据需要来回传递值。不要让你的页面逻辑泄露到你的外部服务中。好吧,但是怎么做呢?从我对第一个答案的评论中可以看出,我试图添加一个传递值的方法,但似乎不起作用。也许我做错了!您是否坚持使用WCF?您需要来回传递什么样的数据?我需要交换字符串数据,比如序列化的json对象。我不是“卡住”了,但因为
WCF
适合我,我愿意接受其他更好的选择,如果它们值得对代码进行进一步修改的话。不管您使用什么,只要在代码中编写代码,以收集外部应用程序所需的必要数据,并将其作为消息的一部分发送即可。