简体   繁体   中英

Use of Page_PreRender to execute C# code when page controls are loaded

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!

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>"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM