Answered by:
Problem with Session[...] = null variable

Question
-
User-941706905 posted
Hello!
When I click a link in the HTML I do set a Session["loadtheiframe"] = "dummy" that I am successfully able to detect in codebehind C# to activate "function clickdownload()" which is a javascript function. This works fine.
However I only want this function to execute once. If I now press F5 to update the page, I do NOT want this "function clickdownload()" to execute again. But it does execute again. I am trying to put the Session["loadtheiframe"] = null in the page load but it doesn't seem to work? Any idéa how I could do this?
Thank you!
protected void Page_Load(object sender, EventArgs e) { if (Session["loadtheiframe"] != null) { Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "clickdownload()", true); Session["loadtheiframe"] = null; } }
<iframe src="" name="iframeName" style="height:0px; width: 0px; font-size: 50px"></iframe> <button id="downloadimagebutton" runat="server" class="button"><span><a onclick="javascript: <% Session["loadtheiframe"] = "dummy"; %>" href="lunch-menu.aspx" target="iframeName" style="color: #D61C22">Download Menu as Image</a></span></button> <a id="btnConvertHtml2Image" runat="server" href="#" style="color: #D61C22"></a> <script> function clickdownload() { setTimeout(function () { document.getElementById('btnConvertHtml2Image').click(); }, 4000); } </script>
Monday, April 9, 2018 5:53 PM
Answers
-
User475983607 posted
This will be to much work as I have 11 different pages and I capture a specific DIV as an image so I will not print the entire page as such. If I in somehow can catch a session variable etc in the Page_Load or simular that will solve the problem entirely. It has to be possible to do this somehow?
I think you should take a moment to read the link before disregarding CSS for printing.
A media query can hide the bits you don't want printed or just print the section you want printed. IMHO, this is by far easier to implement and maintain than your current complex solution.
If you really must implement this overly complicated process, then add a count to Session rather than setting Session to null. Start at zero and increment Session for each print click. If Session is greater than zero then user clicked the print button. From here you can provide a message to the user and ask if they really want to print again.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 9, 2018 7:53 PM -
User-941706905 posted
You are right, your solution is more accurate. I will check on that later on.
However, I think I will try to just stick with this solution for now. I beleive I find a solution by doing this, which actually seems to work. I set the href to: "lunch-menu.aspx?iframe=yes"
Then in Page_Load I check if the loading page has "iframe=yes" in it. By this I then know that it is the iframe that is loading.
It seems to work. Thank you for your help!
<iframe id="iframname2" runat="server" src="" name="iframeName" style="height:0px; width: 0px; font-size: 50px;"></iframe> <a id="downloadimagebutton2" onclick="" href="lunch-menu.aspx?iframe=yes" target="iframeName" style="color: #D61C22">Download Menu as Image</a> string path1 = HttpContext.Current.Request.Url.ToString(); if (path1.Contains("iframe=yes")) { //change font-size }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 9, 2018 9:14 PM
All replies
-
User475983607 posted
The line of code below sets Session every time the page renders. so yes, pressing F5 or simply loading this page will cause clickdownload() to fire.
<a onclick="javascript: <% Session["loadtheiframe"] = "dummy"; %>" href="lunch-menu.aspx" target="iframeName" style="color: #D61C22">Download Menu as Image</a></span></button>
Can you explain what you are trying to do?
Monday, April 9, 2018 6:34 PM -
User-941706905 posted
I didn't know it sets the Session when page renders. I thought it only did it when clicked.
What I try to do is this:
From the lunch-menu.aspx page, I do click the link to open this very same page in the "iframe". The thing is that in that iframe I change some font-sizes of text to look good when printing it. The iframe is not visible for the eye. So then I need the session variable in Page_Load to change fontsizes and that will then only happen in the iframe and not the normal page that is visible for the eye.
Monday, April 9, 2018 7:19 PM -
User475983607 posted
Silvers
I didn't know it sets the Session when page renders. I thought it only did it when clicked.<% %> is an ASP code block that executes on the server.
Silvers
From the lunch-menu.aspx page, I do click the link to open this very same page in the "iframe". The thing is that in that iframe I change some font-sizes of text to look good when printing it. The iframe is not visible for the eye. So then I need the session variable in Page_Load to change fontsizes and that will then only happen in the iframe and not the normal page that is visible for the eye.Explain what you are trying to do at a high level not how you think the problem should be solved from a technical perspective.
Use a CSS media query if you are trying to create a printer friendly page.
https://benfrain.com/create-print-styles-using-css3-media-queries/
Monday, April 9, 2018 7:26 PM -
User-941706905 posted
<% %> is an ASP code block that executes on the server.
Then I understand.
On the page: lunch-menu.aspx, I have this code:
<iframe src="" name="iframeName" style="height:0px; width: 0px; font-size: 50px"></iframe> <a onclick="javascript: <% Session["loadtheiframe"] = "dummy"; %>" href="lunch-menu.aspx" target="iframeName" style="color: #D61C22">Download Menu as Image</a> </a>
Now, I want to open lunch-menu.aspx in the iframe. I do that by clicking the link. This works. Now there are alot of text on lunch-menu.aspx that I can access in Page_Load, for example, I change this text that has 12px hardcoded on the page to 15px. Because the text is needed to be larger when printing to not be to small.
So my problem is that I only want to run some C# code when the iframe is loading. How can I do that?
extrainfodiv.Style["font-size"] = "15px";
Monday, April 9, 2018 7:35 PM -
User475983607 posted
So my problem is that I only want to run some C# code when the iframe is loading. How can I do that?Use a CSS media query to create a printer friendly page.
https://benfrain.com/create-print-styles-using-css3-media-queries/
Monday, April 9, 2018 7:38 PM -
User-941706905 posted
This will be to much work as I have 11 different pages and I capture a specific DIV as an image so I will not print the entire page as such. If I in somehow can catch a session variable etc in the Page_Load or simular that will solve the problem entirely. It has to be possible to do this somehow?
Monday, April 9, 2018 7:40 PM -
User475983607 posted
This will be to much work as I have 11 different pages and I capture a specific DIV as an image so I will not print the entire page as such. If I in somehow can catch a session variable etc in the Page_Load or simular that will solve the problem entirely. It has to be possible to do this somehow?
I think you should take a moment to read the link before disregarding CSS for printing.
A media query can hide the bits you don't want printed or just print the section you want printed. IMHO, this is by far easier to implement and maintain than your current complex solution.
If you really must implement this overly complicated process, then add a count to Session rather than setting Session to null. Start at zero and increment Session for each print click. If Session is greater than zero then user clicked the print button. From here you can provide a message to the user and ask if they really want to print again.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 9, 2018 7:53 PM -
User-941706905 posted
You are right, your solution is more accurate. I will check on that later on.
However, I think I will try to just stick with this solution for now. I beleive I find a solution by doing this, which actually seems to work. I set the href to: "lunch-menu.aspx?iframe=yes"
Then in Page_Load I check if the loading page has "iframe=yes" in it. By this I then know that it is the iframe that is loading.
It seems to work. Thank you for your help!
<iframe id="iframname2" runat="server" src="" name="iframeName" style="height:0px; width: 0px; font-size: 50px;"></iframe> <a id="downloadimagebutton2" onclick="" href="lunch-menu.aspx?iframe=yes" target="iframeName" style="color: #D61C22">Download Menu as Image</a> string path1 = HttpContext.Current.Request.Url.ToString(); if (path1.Contains("iframe=yes")) { //change font-size }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 9, 2018 9:14 PM