Solved

javascript call

Posted on 2016-10-14
6
34 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 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
Sorry to reply late.

Where should I place

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


Thanks
0
 

Author Comment

by:mcrmg
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
thank you
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
md5sum check using powershell v 1.0 4 52
Problem to picture file 3 37
getting id from database 5 23
Asp.net mvc entity issue 6 18
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now