render not rendering literal controls

G

geoff

I'm working on project where I find a control on a page and then
insert a literal control (a tag) directly before it with the
form.controls.addat method. I stepped through the code and used the
immediate window to show all the controls in the form controls
collection, and the literalcontrol that I added was there where it was
supposed to be. But, when the page displayed in the browser, it
wasn't in the html. This event is occurring after the containing
page's oninit and page load events have already fired. Is that why
it's not rendering it because the page's form control collection has
already been rendered to the page and adding more controls at this
point would have no effect? If so, how else could I do this? Thanks.
 
S

Scott

There are lots of places (and events) where you should be able to do this; the important thing is that it's done before the control tree is rendered; below is a sample.

Scott

<%@ Page language="c#" AutoEventWireup="false" trace="true" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<script language="C#" runat="server">
protected override void OnPreRender(EventArgs e)
{
Control c = FindControl("Label1");
LiteralControl l = new LiteralControl("<strong>hello</strong>");
l.ID = "PreLit";
int i = 0;
foreach (Control child in c.Parent.Controls)
{
if (child == c)
{
c.Parent.Controls.AddAt(i, l);
break;
}
++i;
}
}
</script>
</head>
<body>
<form id="Form1" method="post" runat="server">
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label></p>
<p>&nbsp;</p>
<p>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></p>

</form>
</body>
</html>


I'm working on project where I find a control on a page and then
insert a literal control (a tag) directly before it with the
form.controls.addat method. I stepped through the code and used the
immediate window to show all the controls in the form controls
collection, and the literalcontrol that I added was there where it was
supposed to be. But, when the page displayed in the browser, it
wasn't in the html. This event is occurring after the containing
page's oninit and page load events have already fired. Is that why
it's not rendering it because the page's form control collection has
already been rendered to the page and adding more controls at this
point would have no effect? If so, how else could I do this? Thanks.
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top