.net 下动态加载自定义控件后传值及函数调用方法

自定义的用户控件,可通过在页面中直接引入及动态加载使用:

1.直接在页面aspx文件中引用的,可直接在其.cs文件中通过控件ID调用其公共属性及方法:

eg:

自定义控件ITyouhuiCtl:

ITyouhuiCtl.ascx.cs:

定义属性:

private string url;

public string Url 
{ 
    get { return url; } 
    set { url = value; } 
}

public string SetUrl(string urlInput,string para2)//通过函数调用来设置多个变量

{

   url = urlInput;

…

}

页面Default.aspx:

<%@ Register Src="/Controls/ITyouhuiCtl.ascx" TagName="ITyouhuiCtl" TagPrefix="WFO" %>

<WFO:ITyouhuiCtlID="yh" runat="server" />

Default.aspx.cs:

yh.Url = “http://www.ityouhui.com”;//直接引用

2.动态加载自定义控件引入的,函数调用方法:

对于函数调用:

eg:

Default.aspx:

<asp:Panel ID="extPL1" runat="server" Visible ="false" />

Daefault.aspx.cs:

Control c = Page.LoadControl(Request.ApplicationPath + "/Controls/ITyouhuiCtl.ascx");//动态加载控件 
Type tc = c.GetType(); 
System.Reflection.MethodInfo m = tc.GetMethod("SetUrl"); //xx为控件中函数 
object[] bjParas = new object[2]; 
bjParas[0] = “http://www.ityouhui.com/channel/notebook”;//控件中函数参数 
bjParas[1] = "seller"; 
m.Invoke(c, bjParas);//调用 
extPL1.Controls.Add(c);//将控件添加到面板占位处

3.动态加载自定义控件,调用属性:

首先需要在自定义控件中申明类名:

eg:

自定义控件ITyouhuiCtl需添加ClassName属性:

ITyouhuiCtl.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ITyouhuiCtl.ascx.cs" ClassName="ITyouhuiCtl" Inherits="Controls_ITyouhuiCtl" %>

在引用页Default.aspx中注册:

<%@ Register src="/Controls/ITyouhuiCtl.ascx" tagname="ITyouhuiCtl" tagprefix="WFO" %>

在Default.aspx.cs中即可直接使用:

ASP.ITyouhuiCtl c; 
c = (ASP.ITyouhuiCtl)(Page.LoadControl(Request.ApplicationPath + "/Controls/ITyouhuiCtl.ascx")); 
extPL1.Controls.Add(c); 
c.Url = “http://www.ityouhui.com/channel/camera”;//直接调用!

//同样,这里可以调用函数,更为简单

注:ITyouhuiCtl 前一般是ASP.的名字空间,当然各人配置不一样可能有差异,不知道的情况下,可在前面几步完成之后,调用处直接输入控件名ITyouhuiCtl ,将鼠标移上去,vs会提示添加名字空间,加上即可;

OVER!

ORG:http://blog.donews.com/me1105/archive/2011/05/15/154.aspx

转载于:https://www.cnblogs.com/me115/archive/2011/05/15/2046873.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET Core 并不直接支持调用 WinForms 自定义控件,因为 ASP.NET Core 是基于跨平台的开发框架,而 WinForms 是一个基于 Windows 平台的桌面应用程序框架。不过,你可以通过一些技巧来实现在 ASP.NET Core 中使用 WinForms 控件。 一种常见的做法是使用 SignalR 实现实时通信,将 WinForms 控件的 UI 渲染在服务器端,并通过 SignalR 将渲染后的 UI 推送给客户端。这样,客户端就可以通过浏览器访问 ASP.NET Core 应用程序并查看 WinForms 控件的渲染结果。 下面是一个简单的示例,演示如何在 ASP.NET Core 中使用 WinForms 控件: 1. 创建一个 ASP.NET Core Web 应用程序项目。 2. 在项目中添加对 `Microsoft.AspNetCore.SignalR` 的引用。 3. 创建一个名为 `WinFormsService` 的类,用于处理 WinForms 控件的渲染和更新: ```csharp using System; using System.Drawing; using System.Windows.Forms; public class WinFormsService { private readonly HubContext<WinFormsHub> _hubContext; private readonly Control _winFormsControl; public WinFormsService(HubContext<WinFormsHub> hubContext) { _hubContext = hubContext; _winFormsControl = new YourCustomControl(); // 替换为你自己的自定义控件类型 _winFormsControl.Size = new Size(300, 300); _winFormsControl.Paint += WinFormsControl_Paint; } private void WinFormsControl_Paint(object sender, PaintEventArgs e) { // 在控件的绘制事件中渲染 UI,并将渲染结果发送给客户端 using (var bitmap = new Bitmap(_winFormsControl.Width, _winFormsControl.Height)) { _winFormsControl.DrawToBitmap(bitmap, _winFormsControl.ClientRectangle); var imageData = ImageToBase64(bitmap); _hubContext.Clients.All.SendAsync("UpdateWinFormsUI", imageData); } } private string ImageToBase64(Image image) { using (var memoryStream = new MemoryStream()) { image.Save(memoryStream, ImageFormat.Png); var imageDataBytes = memoryStream.ToArray(); return Convert.ToBase64String(imageDataBytes); } } } ``` 4. 创建一个名为 `WinFormsHub` 的 SignalR Hub 类,用于处理客户端的连接和消息: ```csharp using Microsoft.AspNetCore.SignalR; public class WinFormsHub : Hub { public async Task JoinGroup(string groupName) { await Groups.AddToGroupAsync(Context.ConnectionId, groupName); } public async Task LeaveGroup(string groupName) { await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName); } } ``` 5. 在 `Startup.cs` 文件的 `ConfigureServices` 方法中注册服务: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddSignalR(); services.AddSingleton<WinFormsService>(); } ``` 6. 在 `Startup.cs` 文件的 `Configure` 方法中启用 SignalR 终结点: ```csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // ... app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapHub<WinFormsHub>("/winformsHub"); // SignalR 终结点 endpoints.MapControllers(); }); } ``` 7. 创建一个名为 `WinFormsController` 的控制器,用于处理客户端的请求: ```csharp [Route("api/[controller]")] [ApiController] public class WinFormsController : ControllerBase { private readonly WinFormsService _winFormsService; public WinFormsController(WinFormsService winFormsService) { _winFormsService = winFormsService; } [HttpGet("start")] public IActionResult Start() { // 启动 WinForms 控件渲染 _winFormsService.StartRendering(); return Ok(); } } ``` 8. 在客户端的 HTML 页面中使用 SignalR 连接并接收来自服务器的渲染结果: ```html <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="/winformsHub/hubs"></script> <script> const connection = new signalR.HubConnectionBuilder() .withUrl("/winformsHub") .build(); connection.on("UpdateWinFormsUI", function (imageData) { // 接收来自服务器的渲染结果,并将其显示在页面上 const imageElement = document.getElementById("winformsImage"); imageElement.src = "data:image/png;base64," + imageData; }); connection.start().then(function () { connection.invoke("JoinGroup", "WinFormsGroup"); }); </script> <img id="winformsImage" src="" alt="WinForms UI"> ``` 以上示例中,`WinFormsService` 类负责渲染 WinForms 控件的 UI,并使用 SignalR 将渲染结果推送给客户端。`WinFormsHub` 类是 SignalR 的 Hub 类,用于处理客户端的连接和消息。`WinFormsController` 类是一个 ASP.NET Core 控制器,用于启动 WinForms 控件的渲染。 请注意,上述示例仅为演示目的,实际应用中可能需要根据具体需求进行修改和扩展。此外,这种方法可能会带来一些性能和安全方面的考虑,请根据实际情况进行评估和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值