Link to home
Start Free TrialLog in
Avatar of mcrmg
mcrmg

asked on

javascript call

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

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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.
Avatar of mcrmg
mcrmg

ASKER

Sorry to reply late.

Where should I place

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


Thanks
Avatar of mcrmg

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcrmg

ASKER

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
Avatar of mcrmg

ASKER

thank you