Solved

javascript call

Posted on 2016-10-14
6
64 Views
Last Modified: 2016-10-31
Hi,

For some reason, the code is only "accurate" for the first time you run it. After the second time, it is showing the value that submitted from the previous time, kind of confused.  thanks

    <script type = "text/javascript">
        function Confirm() {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm("Report name exists already. Do you want to replace this report?")) {
                confirm_value.value = "Yes";
            } else {
                confirm_value.value = "No";
            }
            document.forms[0].appendChild(confirm_value);
        }
    </script>

 <asp:Button id="SaveReportbtn" onclick="SaveReportbtn_Click"  CustomParameter="listBox1" runat="server" text="Save Report" />Report" />

Open in new window



                    ScriptManager.RegisterStartupScript(this, GetType(), "myFunction", "Confirm();", true);     

                    string confirmValue = Request.Form["confirm_value"];
                    if (confirmValue == "Yes")
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
                     
                    }
                    else
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
                     


                    }

Open in new window

0
Comment
Question by:mcrmg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41844417
Every time you click the button a new hidden input is being created.
    <script type = "text/javascript">
        function Confirm() {
            // CREATED HERE
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm("Report name exists already. Do you want to replace this report?")) {
                confirm_value.value = "Yes";
            } else {
                confirm_value.value = "No";
            }
             // ADDED HERE
            document.forms[0].appendChild(confirm_value);
        }
    </script>

Open in new window

So what ends up happening is this
<input type="hidden" name="confirm_value" value="Yes" />
<input type="hidden" name="confirm_value" value="No" />

Open in new window

You will have two controls with confirm_value as the name - only one will win on the server side - which is probably why you are seeing an "older" value.

Two options
1. Don't create the element dynamically - put it on the page with an empty value and an id - then simply set the value by id
document.getElementById('confirm_value').value = 'Yes';

Open in new window


2. This is only an option if you want the entire history. Give each new instance of the control a unique name so that you can retrieve the values on the server side.

I am thinking that 1 is probably the valid answer here.
0
 

Author Comment

by:mcrmg
ID: 41846885
Sorry to reply late.

Where should I place

document.getElementById('confirm_value').value = 'Yes';


Thanks
0
 

Author Comment

by:mcrmg
ID: 41848304
This is my code, for some reason, it is sending empty value.  Thank you

    <script type = "text/javascript">
        function Confirm() {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm("Report name exists already. Do you want to replace this report?")) {
                //confirm_value.value = "Yes";
                document.getElementById('confirm_value').value = 'Yes';
                //alert('111');
            } else {
            //confirm_value.value = "No";
            document.getElementById('confirm_value').value = 'No';
            //alert('000');
            }
            
           // document.forms[0].appendChild(confirm_value);
            
        }
    </script>

<form>
<input type="text" id="confirm_value" value="">
</form>

Open in new window


code behind
                    ScriptManager.RegisterStartupScript(this, GetType(), "myFunction", "Confirm();", true);     

                    string confirmValue = Request.Form["confirm_value"];
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(" + confirmValue + ")", true);
                    
                    if (confirmValue == "Yes")
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
                    }
                    else
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);


                    }

Open in new window

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 58

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41848339
Take out lines 3-5 of the first listing
Add a hidden input to the page with id=confirm_value and name=confirm_value
0
 

Author Comment

by:mcrmg
ID: 41850476
I think I found out the problem. But needs direction on how to solve this.

When a button is clicked, it calls the function in the code-behind first, then call the javascript function before the YES/NO is set.  

How can I cal javascript first then pass it to code behind?  thanks
0
 

Author Closing Comment

by:mcrmg
ID: 41867218
thank you
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question