After LoadControl, can updated Literal, but not Textbox?

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:panel 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;
}
}
}
------------------------------------------------------------------------------------------------------------
 
S

Stan

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 kind of description doesn't help when it comes to programming.
What do you mean by "doesn't know" or "doesn't care"?
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?

Have you tried viewing the page source code from the browser (using
the view source menu option)? You might find the text box is there but
the surrounding html is not well formed and so the browser isn't
showing it. There is always this danger when using a literal control.
BTW why not use a label control?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top