render not rendering literal controls

Discussion in 'ASP .Net Web Controls' started by geoff, Apr 15, 2004.

  1. geoff

    geoff Guest

    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.
     
    geoff, Apr 15, 2004
    #1
    1. Advertising

  2. geoff

    Scott Guest

    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>


    "geoff" <> wrote in message news:...
    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.
     
    Scott, Apr 17, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lau Lei Cheong

    Page.Render do not render complete page

    Lau Lei Cheong, May 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,122
    Lau Lei Cheong
    May 15, 2004
  2. =?Utf-8?B?Sm9l?=

    Rendering Server Controls in literal text

    =?Utf-8?B?Sm9l?=, Oct 24, 2005, in forum: ASP .Net
    Replies:
    14
    Views:
    696
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 24, 2005
  3. Stephen Miller
    Replies:
    1
    Views:
    306
    Alessandro Zifiglio
    Jan 2, 2004
  4. Stephen Miller
    Replies:
    12
    Views:
    836
    Stephen Miller
    Jan 20, 2004
  5. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    413
    Anonieko Ramos
    Sep 27, 2004
Loading...

Share This Page