S
Sean
I've got a web page that holds a user control, which is really a
factory that might load up one of several different user controls
inside it. The web page doesn't really know what kind of control is
getting loaded, and it doesn't care. It just passes the data in to the
factory/container control and trusts that control to load up the right
control for the content provided.
This all works great, and I've used it in multiple projects. But I've
always just had the child controls hold literals. They never held any
interactive controls like a textbox. And now, I need them to do that,
and I've discovered something that doesn't work the way I expected.
I'll post the code below, but basically, here's what's happening:
The default page has a textbox, a button, and a factory control. You
enter text into the textbox and hit the button.
What should happen is that the page reloads. Then the page takes the
text from the textbox and hands it to the factory control. The factory
control loads the child control, and passes that text to the child
control to display. The child control then displays that text twice -
once in a textbox and once in a literal.
If you set a breakpoint in the child control, you can even see that
the textbox.text shows the correct value. BUT, when the page loads,
only the literal will actually display the value. The textbox itself
is blank. Why is it doing that?
Here's the code:
------------------------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="Dirtbox._Default" %>
<%@ Register src="~/FactoryControl.ascx" TagName="edit"
TagPrefix="admin" %>
<html>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="sourceText" runat="server"/>
<asp:Button id="myButton" runat="server" Text="Update User
Control"/>
<admin:edit runat="server" id="containerControl"/><br />
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------
using System;
namespace Dirtbox
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
containerControl.PutStuffIntoUserControl(sourceText.Text);
}
}
}
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="FactoryControl.ascx.cs" Inherits="Dirtbox.FactoryControl"
%>
<asp
anel ID="myPanel" runat="server"/>
------------------------------------------------------------------------------------------------------------
namespace Dirtbox
{
public partial class FactoryControl : System.Web.UI.UserControl
{
public void PutStuffIntoUserControl(string useThis)
{
MyUserControl subControl = (MyUserControl)LoadControl("~/
MyUserControl.ascx");
subControl.myText = useThis;
subControl.PopulateInteriorControl();
myPanel.Controls.Add(subControl);
}
}
}
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="MyUserControl.ascx.cs" Inherits="Dirtbox.MyUserControl"
EnableViewState="false" %>
<asp:TextBox ID="textWithinControl" runat="server"/><br />
<asp:Literal ID="literalWithinControl" runat="server"/>
------------------------------------------------------------------------------------------------------------
namespace Dirtbox
{
public partial class MyUserControl : System.Web.UI.UserControl
{
public string myText;
public void PopulateInteriorControl()
{
textWithinControl.Text = myText;
literalWithinControl.Text = myText;
}
}
}
------------------------------------------------------------------------------------------------------------
factory that might load up one of several different user controls
inside it. The web page doesn't really know what kind of control is
getting loaded, and it doesn't care. It just passes the data in to the
factory/container control and trusts that control to load up the right
control for the content provided.
This all works great, and I've used it in multiple projects. But I've
always just had the child controls hold literals. They never held any
interactive controls like a textbox. And now, I need them to do that,
and I've discovered something that doesn't work the way I expected.
I'll post the code below, but basically, here's what's happening:
The default page has a textbox, a button, and a factory control. You
enter text into the textbox and hit the button.
What should happen is that the page reloads. Then the page takes the
text from the textbox and hands it to the factory control. The factory
control loads the child control, and passes that text to the child
control to display. The child control then displays that text twice -
once in a textbox and once in a literal.
If you set a breakpoint in the child control, you can even see that
the textbox.text shows the correct value. BUT, when the page loads,
only the literal will actually display the value. The textbox itself
is blank. Why is it doing that?
Here's the code:
------------------------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="Dirtbox._Default" %>
<%@ Register src="~/FactoryControl.ascx" TagName="edit"
TagPrefix="admin" %>
<html>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="sourceText" runat="server"/>
<asp:Button id="myButton" runat="server" Text="Update User
Control"/>
<admin:edit runat="server" id="containerControl"/><br />
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------
using System;
namespace Dirtbox
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
containerControl.PutStuffIntoUserControl(sourceText.Text);
}
}
}
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="FactoryControl.ascx.cs" Inherits="Dirtbox.FactoryControl"
%>
<asp
------------------------------------------------------------------------------------------------------------
namespace Dirtbox
{
public partial class FactoryControl : System.Web.UI.UserControl
{
public void PutStuffIntoUserControl(string useThis)
{
MyUserControl subControl = (MyUserControl)LoadControl("~/
MyUserControl.ascx");
subControl.myText = useThis;
subControl.PopulateInteriorControl();
myPanel.Controls.Add(subControl);
}
}
}
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="MyUserControl.ascx.cs" Inherits="Dirtbox.MyUserControl"
EnableViewState="false" %>
<asp:TextBox ID="textWithinControl" runat="server"/><br />
<asp:Literal ID="literalWithinControl" runat="server"/>
------------------------------------------------------------------------------------------------------------
namespace Dirtbox
{
public partial class MyUserControl : System.Web.UI.UserControl
{
public string myText;
public void PopulateInteriorControl()
{
textWithinControl.Text = myText;
literalWithinControl.Text = myText;
}
}
}
------------------------------------------------------------------------------------------------------------