将消息从网页发送到webcontrol

本文关键字:webcontrol 网页 消息 | 更新日期: 2023-09-27 18:08:57

目标:
在Web控件中显示消息。消息将在default.aspx 中启动

问题:

我不知道如何发送消息(字符串test="testing testing"(使用C#代码从Default.aspx到WebUserControl.ascx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%@ Register Tagprefix="Acme" Tagname="AdRotator" Src="~/WebUserControl1.ascx" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <Acme:AdRotator id="Control1" runat="server" />
</asp:Content>

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>

namespace WebApplication1
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

//Fullmetalboy

将消息从网页发送到webcontrol

不确定我是否完全理解这个问题。您可以在Bag控件上公开一个公共属性,用于设置/获取Coffee实例。

如果没有Bag控件的直接句柄,则可以始终在当前HttpContext项缓存中传递它。

但是,如果真的想转向松耦合和依赖注入,那么就在WebForms中实现MVP模式。

编辑:根据您更新的问题,向您的Web控件添加一个属性,如下所示。

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    public string MyProperty
    {
       get;set;
    }
}

然后从页面设置属性,如下所示:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Control1.MyProperty = "Testing Testing";
    }
}

或者像这样:

<Acme:AdRotator id="Control1" runat="server" MyProperty="Testing Testing" />