1

I have a file called Upload that is used to upload images. Other files then call this Upload file. The problem is I need to know which upload button was clicked, because one button needs to save the images in a different path file than the other upload buttons. I tried creating a session but the session was being created each time the page was loaded so that won't work. The code that handles the upload is in an iframe. When I click the upload button, the iframe appears which gives the option to select a file from your computer and upload it. I need to pass a value from the ShowUploadImage() function to the iframe so I know which upload button was clicked.

This is code behind the upload button:

 function ShowUploadImage() {
        $('#dvAddEditImage').fadeIn(300);
        $("#dvAddImage").html("<img src=\"images/loading.gif\" alt=\"Loading...\" title=\"Loading...\" />");
        $("#dvAddImage").fadeIn(300);
        $("#dvAddImage").html("<iframe id=\"iframeUpload\" src='utilities/UploadPOD.aspx'></iframe>");
        Session["Session"] = "Upload";
    }

I added the Session to Page_Load. But now the session is created every time which isn't what I need.

protected void Page_Load(object sender, EventArgs e)
    {
            Session["Session"] = "Upload";
        }

    }

Then in the Upload file I need to check which button was clicked:

if (fuUpload.HasFile)
            {
                string JobType = Convert.ToString(Session["Session"]);
                Session["Session"] = null;
                if (JobType == "Upload")
                {

                }
                else
                {

                }
           }

Maybe using a session isn't the right way to do this? How do I pass a value over between different code files?

EDIT

Ok added a hidden field:

<asp:HiddenField ID="hfSession" runat="server" Value="Upload" />

And added to the upload button:

function ShowUploadImage() {
        $('#dvAddEditImage').fadeIn(300);
        $("#dvAddImage").html("<img src=\"images/loading.gif\" alt=\"Loading...\" title=\"Loading...\" />");
        $("#dvAddImage").fadeIn(300);
        $("#dvAddImage").html("<iframe id=\"iframeUpload\" src='utilities/UploadPOD.aspx'></iframe>");
        document.getElementById('<%= hfSession.ClientID %>').value = "Upload"; 
    }

So how do I call this hidden field in the other page file. These 2 files have no connection so how is the value of the hidden field going to be passed?

user123456789
  • 1,914
  • 7
  • 44
  • 100
  • Why wont you send a param to this Upload page with the desired path? Like create your iframe like this: `` and then generate a `hidden` field with this param... – DontVoteMeDown Aug 24 '15 at 14:33
  • "session was being created each time the page was loaded so that won't work." It shouldn't. Also many ways to pass the trigger. E.g cache, cookies, & passing it as param between pages.... – Jared Teng Aug 24 '15 at 14:35
  • @DontVoteMeDown UploadPOD.aspx is the target path. Other upload buttons use the same path so I need a way to check which upload button was clicked – user123456789 Aug 24 '15 at 14:36
  • @JaredT I only want the session to be created when the upload button was clicked. If it gets created every time the page is loaded then I can't tell which uploaded button was clicked – user123456789 Aug 24 '15 at 14:37
  • Just add a hidden field to the form and populate it on button click, with an identifier that tells you which button it was. You can check that server-side and react accordingly. – Reinstate Monica Cellio Aug 24 '15 at 14:38
  • @Archer could you show some code on how to check the hidden field on the server-side? – user123456789 Aug 24 '15 at 14:41
  • In fact, I can't be sure as there's only snippets of code above, but if you get rid of the code that sets the session variable at page load then it will probably solve your problem. – Reinstate Monica Cellio Aug 24 '15 at 14:46
  • @Archer I tried that but then the session doesn't created. Please see my edit. I added code for the hidden field – user123456789 Aug 24 '15 at 14:49
  • @JaredT Please see my edit. I added code for the hidden field – user123456789 Aug 24 '15 at 14:50
  • Sorry, but you're not explaining clearly so it's very difficult to understand. It sounds now as if the page with the buttons on is not the page that handles the uploads. Which page did you put the hidden field in, and is it in an iframe as suggested above? There's lots of questions here that need to be answered. Can you make a simple piece of code that outlines your issue exactly? – Reinstate Monica Cellio Aug 24 '15 at 14:51
  • @Archer The page that handles the file upload is a separate file (an iframe). Another file then contains the upload button. When the upload button is clicked it opens the iframe ` $("#dvAddImage").html("");` The iframe then handles the upload code – user123456789 Aug 24 '15 at 14:53
  • @JaredT the session is null when it goes into the if statement on the upload page. So does that mean the value isn't getting passed to the session in the ShowUploadImage() function? – user123456789 Aug 24 '15 at 15:05
  • You can't set a server-side session variable with Javascript alone. Do you have a similar piece of code for making the other button? – Reinstate Monica Cellio Aug 24 '15 at 15:07
  • @Archer Yes the code is the exact same for the other button. That is why I need to add code to see which button has been clicked. – user123456789 Aug 24 '15 at 15:09
  • @JaredT but the session is equal to null. I just want the session to be created when the upload button is pressed. The session should get its value when the button is clicked – user123456789 Aug 24 '15 at 15:10
  • Simplest solution I can't think of, since I'm not familiar with the whole implementation of your system, is cookies. Read answers from here http://stackoverflow.com/questions/2257631/how-create-a-session-using-javascript. You can also trash the data in the cookie every reload to make sure the saved trigger is fresh. – Jared Teng Aug 24 '15 at 15:18

2 Answers2

2

Add something to the URL of the iframe, but make it different for each of the buttons...

function ShowUploadImage() {
    $('#dvAddEditImage').fadeIn(300);
    $("#dvAddImage").html("<img src=\"images/loading.gif\" alt=\"Loading...\" title=\"Loading...\" />");
    $("#dvAddImage").fadeIn(300);
    $("#dvAddImage").html("<iframe id=\"iframeUpload\" src='utilities/UploadPOD.aspx?id=upload1'></iframe>");
}

Then in your server-side code, check the value passed in the querystring...

if (Request.QueryString["id"] == "upload1")
{
    // do something here
}

Do the same for the other button, but pass a different value in the querystring.

Reinstate Monica Cellio
  • 25,975
  • 6
  • 51
  • 67
0

If you just want to pass value of the button clicked from one page to another, maybe you could try using sessionStorage in your ShowUploadImage() method. set a value in that method and retrieve the same thing in another page

something like this

function ShowUploadImage() {
    $('#dvAddEditImage').fadeIn(300);
    $("#dvAddImage").html("<img src=\"images/loading.gif\" alt=\"Loading...\" title=\"Loading...\" />");
    $("#dvAddImage").fadeIn(300);
    $("#dvAddImage").html("<iframe id=\"iframeUpload\"  src='utilities/UploadPOD.aspx'></iframe>");

    sessionStorage.setItem("action", "Upload");
}

and then on the other page write this wherever you need to fetch it in javascript

sessionStorage.getItem("action");
Omkar Kulkarni
  • 784
  • 6
  • 12