Asked by:
IFrame Auto Height & Pass the URL a parameter

Question
-
User240536124 posted
OK, I'm weak on javascript. I have two forum posts that worked great for me, but not at the same time.
iframe auto height & How to pass parameters to an IFRAME
Basically, the iframe with auto height is like this:
<iframe src="YourPage.aspx" width="100%" height="200px" id="iframe1" marginheight="0" frameborder="0" onload='javascript:resizeIframe(this);'></iframe>
Calls resizeIframe(this); Worked first try.
I need to pass YourPage.aspx some parameters. Solution:
<iframe id="iframe1" runat="server">
In C#
iframe1.Attributes["src"] = "test.aspx?param=" + Request.QueryString["name"];
When I add the tags to my iframe as per the second forum post, the javascript quits working from the first solution: resizeIframe(this);
I get this error: BC30456: 'javascript' is not a member of 'ASP.circle_sales_order_nav_aspx'.
I tried to add the onload like this: iframe1.Attributes["onload"] =
The error goes away but the auto resize doesn't work.
Can anyone point me in a direction to get both of these solutions working at the same time? Would be perfect for what I need to do. Or another way that does both???
Thanks
Friday, February 1, 2019 5:30 PM
All replies
-
User839733648 posted
Hi jay8anks,
Acccording to your description, I'm sorry that I could not reproduce your issue clearly.
First, the two link is the same.
iframe1.Attributes["src"] = "test.aspx?param=" + Request.QueryString["name"];Then what does the name mean? Where does the name value come from?
If you want to pass some parameters to the iframe src, you may write something like below in your js code.
document.getElementById("IframeId").src = 'link' + params;
Besides, if possible, please provide more details like detailed related codes so that it will be easier for us to help with you.
The following two link may be helpful to you.
https://stackoverflow.com/questions/8642488/how-to-use-javascript-variable-in-iframe-src
Best Regards,
Jenifer
Monday, February 4, 2019 7:14 AM -
User240536124 posted
Sorry,
The second link was: https://forums.asp.net/t/1480281.aspx?How+to+pass+parameters+to+an+IFRAME+
It is in .aspx = <iframe id="iframe1" runat="server">
In C# iframe1.Attributes["src"] = "test.aspx?param=" + Request.QueryString["name"];
-=-=-
(I may be mixing some C# and VB syntax here, because the page I'm actually working in is VB at the moment).
string MyValue = Request.QueryString("oid").ToString();
I just modified "name" to something like this in my codebehind: iframe1.Attributes("src") = ("printorder.aspx?oid=" + MyValue)
-=-=-
The problem is, when I do this, auto-setting the height and width (as per the first link), quits working and throws the error mentioned in my question.
The above syntax may be screwy, but it does do exactly what I needed it to do, and from codebehind, which I'm much more comfortable in. :)
Monday, February 4, 2019 3:48 PM -
User839733648 posted
Hi jay8anks,
I still suggest that you could set the src in the javascript too.
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="https://code.jquery.com/jquery-3.3.1.js"></script> </head> <body> <form id="form1" runat="server"> <div> <iframe id="iframe1" marginheight="0" frameborder="0" onload="javascript:resizeIframe(this)"></iframe> </div> <script type="text/javascript"> $(function () { $("#iframe1").attr("src", "Test.aspx");//Test.aspx you could customize }) function resizeIframe(obj) { //Declaring variables var newheight; var newwidth; if (document.getElementById) { //Calculating new height newheight = obj.contentWindow.document.body.scrollHeight; //Calculating new width newwidth = obj.contentWindow.document.body.scrollWidth; } //Assigning calculated height to iframe obj.height = (newheight) + "px"; //Assigning calculated width to iframe obj.width = (newwidth) + "px"; } </script> </form> </body> </html>
Or you could add the src and onload the function both in code-behind.
protected void Page_Load(object sender, EventArgs e) { iframe1.Attributes["src"] = "Test.aspx"; iframe1.Attributes["onload"] = "javascript:resizeIframe(this)"; }
Best Regards,
Jenifer
Wednesday, February 13, 2019 4:40 AM