Sending a message from webpage to webcontrol
Goal:
Display a message in the Webcontrol. The message's will be started in the default.aspxProblem:
I don't know how to send a message (string test = "testing testing") from Default.aspx to the WebUserControl.ascx by using C# code
<%@ 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" ContentPlaceHolder开发者_如何学PythonID="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
Not sure I fully understand the question. You can expose a public property on the Bag control that lets you set/get the Coffee instance.
If you don't have a direct handle to the Bag control you can always pass it in the current HttpContext Items cache.
However if you really want to move to loose coupling and dependency injection then implement the MVP pattern in WebForms.
Edit: Based on your updated question add a property to your webcontrol like this.
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public string MyProperty
{
get;set;
}
}
Then set the property from the page like this:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Control1.MyProperty = "Testing Testing";
}
}
Or like this:
<Acme:AdRotator id="Control1" runat="server" MyProperty="Testing Testing" />
精彩评论