0

I have the following code :

protected void Page_Load(object sender, EventArgs e)
{
  string runat = "runat=\"server\"";    
}

protected void Page_PreRender(object sender, EventArgs e)
{       
//some code       
}

<iframe <%=runat%>></iframe>

I need to be sure the code within Page_PreRender executes only when the variable runat has been initialized & the iframe control is ready for rendering. Unfortunately it does not work. I also tried Page_PreRenderComplete and it does not work.

Does anyone have an idea to fix this problem ? Thanks!

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
  • Maybe take a look at this post? http://stackoverflow.com/a/27041430/3231537 – Icepickle Aug 02 '16 at 13:57
  • thank you but i'm using .NET v3.5 –  Aug 02 '16 at 14:09
  • I think you misunderstand the ASP.NET page life cycle. Page_PreRender occurs after Page_Load. https://msdn.microsoft.com/en-us/library/ms178472.aspx – idream1nC0de Aug 02 '16 at 14:41
  • Why are you trying to create a server control by using a code block? I don't think what you're trying to do is possible. Why not just set the runat="server" attribute on the iframe element at design time? Then you could get access to all of the controls on the page in the Page_Load event. – idream1nC0de Aug 02 '16 at 14:58
  • Jacob Heater : Indeed, that's easier but i'm using a CMS which does not allow those attributes easily. I have no other choice than doing it that way unfortunately –  Aug 02 '16 at 15:18

1 Answers1

1

Create a PlaceHolder in your markup and then add the iframe programmatically to the PlaceHolder as a LiteralControl, like this:

protected void Page_Load(object sender, EventArgs e)
{
    PlaceHolder1.Controls.Add(new LiteralControl("<iframe src='something.aspx'></iframe>"));
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175