I have an iframe that has a dynamic URL based on the results of web API call. What is the best way of setting its src in the aspx page? Here is an example of the iframe:
<iframe id="dynamicframe" runat="server"></iframe>
Should I set it directly in the codebehind like so:
dynamicframe.Attributes["src"] = "http://dynamicurl.com";
Or should I create a property in the codebehind and reference it in the iframe:
<iframe id="dynamicframe" src="<%= dynamicFrameUrl %>"></iframe>
Or some other method altogether?
This is a general question that can stands the same for any html tag.
The alternative third option is to use a literal control and fully render the iframe on code behind as:
txtLiteral.Text = "string.Format(
"<iframe id=\"dynamicfrmae\" src=\"{0}\"></iframe>", PageUrlForSrc );
The different for all methods :
<%= %>
This is the method that I avoid most. I use it only when I like to left some calculations for later and avoid page cycle, or when I have responce.flush()
just before it.
All methods have their purpose, and I used then according what they fit best.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With