Solved

javascript call

Posted on 2016-10-14
6
47 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
  • 4
  • 2
6 Comments
 
LVL 54

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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 54

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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