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
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" />
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);
}
ASKER
Sorry to reply late.
Where should I place
document.getElementById('c onfirm_val ue').value = 'Yes';
Thanks
Where should I place
document.getElementById('c
Thanks
ASKER
This is my code, for some reason, it is sending empty value. Thank you
code behind
<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>
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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
thank you
Open in new window
So what ends up happening is thisOpen 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
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.