Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

javascript call

Posted on 2016-10-14
6
Medium Priority
?
72 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 59

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 59

Accepted Solution

by:
Julian Hansen earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

704 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