Javascript Popup from Code Behind Without Button Onclick Action - javascript

I have read numerous threads and cannot figure this one out as they all relate to calling a javascript with a button click. I have a simple javascript popup that I want to call during my vb code in an asp.net project. Each time I call the javascript, the program continues on, and my variable from the popups do not get calculated.
I need the program to stop, wait for the popup reply, and then based upon the reply, continue on. What am I missing? The first one runs fine as it is conditional based upon a button OnClientClick method, but the next two are in the code behind. I need the program to stop, wait for a response, and continue based upon the response. This doesn't happen and using ScriptManager.RegisterStartupScript or other javascript calls as they aren't stopping the program to wait for the response. The second two do not have a button click event to trigger them, only a programming code call.
HTML Code:
<script type="text/javascript">
function Payment() {
var payment_value = document.createElement("INPUT");
payment_value.type = "hidden";
payment_value.name = "payment_value";
if (confirm("Are you sure you want To Cancel this Payment?")) {
payment_value.value = "Yes";
} else {
payment_value.value = "No";
}
document.forms[0].appendChild(payment_value);
}
</script>
<script type="text/javascript">
function LateFee() {
var latefee_value = document.createElement("INPUT");
latefee_value.type = "hidden";
latefee_value.name = "latefee_value";
if (confirm("This payment is over 30 days, do you want to add a Late Fee ($99.00) to this incident?")) {
latefee_value.value = "Yes";
} else {
latefee_value.value = "No";
}
document.forms[0].appendChild(latefee_value);
console.log(latefee_value)
}
</script>
<script type="text/javascript">
function LateFeePrint() {
var latefeeprint_value = document.createElement("INPUT");
latefeeprint_value.type = "hidden";
latefeeprint_value.name = "latefeeprint_value";
if (confirm("Late Fee Updated Successfully, do you want to print a Late Fee Notice?")) {
latefeeprint_value.value = "Yes";
} else {
latefeeprint_value.value = "No";
}
document.forms[0].appendChild(latefeeprint_value);
}
</script>
<h2><%: Title %></h2>
<p class="text-danger">
<asp:Literal runat="server" ID="Literal1" />
</p>
VB,net Code:
If NumDays > 30 And TempLateFee <> "$99.00" Then
Dim LateFeeValue As String = Request.Form("latefee_value")
'Call Javascript and popup the modal
ScriptManager.RegisterStartupScript(Me, Page.GetType, "LateFeeCall", "LateFee()", True)
'use the result to calculate the late fee
If LateFeeValue = "No" Then
ElseIf LateFeeValue = "Yes" Then
AccountBalance = 0
Call UpdateLateFee()
Call GetAlarmInfo()
Call LoadAlarmInfo()
Call LoadBalanceInfo()
End If
End If

Related

Setting global variable value in dynamically created JS function and reading the value in setTimeout callback

I have the following IIFE which adds a function to the DOM on page load. Expectation is that if the user clicks the button within 15seconds of the page load, then the submitForm() function should be executed and the submitAutoDelay function should go into the if block and not show the alert from else block. But unfortunately, it is not working that way. If I click the button right after page load, I see the alert from the submitForm() button, but then after 15sec of page load, I see the alert from the auto submit function as well.
(function(output) {
var autoSubmitDelay = 15000,
submitted = false;
function submitAutoDelay() {
if (submitted) {
console.log("Form already submitted")
return;
} else {
console.log("Submitting form due to timeout");
submitted = true;
console.log("form submitted from timer function");
alert("form submitted from timer function");
}
};
var cbScript = document.createElement("script");
cbScript.type = "text/javascript";
cbScript.text = "function submitform() {" +
" console.log(\"callback called\"); " +
" submitted = true; " +
" console.log(\"form submitted from callback function\");" +
" alert(\"form submitted from callback function\");" +
"}";
document.body.appendChild(cbScript);
setTimeout(submitAutoDelay, autoSubmitDelay);
})(document.forms[0].elements['nadaaaaa']);
<p> </p>
<p> </p>
<form name="myForm">
<button type="button" onclick="submitform();">Click me</button>
</form>
It seems like the scope of the submitted attribute seems to be causing an issue. The value set from the submitForm() method is not being read by the other method. How do I fix this issue?
--- Update---
Looks like I need to define the submitted variable outside of the IIFE so that all functions have access to that variable. It seems to work if I do it that way. I will leave the question open to see if there are other options/solutions for this problem.

onclick() and multiple depending function calls

This code is what i got so far:
<html>
<head>
<script>
function confirmer(what) {
var retVal = confirm("Do you want "+what+"?");
if( retVal == true ) {
document.write ("User wants "+what);
return true;
} else {return false;}
}
</script>
</head>
<body>
<form>
<input type="button" onclick="confirmer('a'); confirmer('b'); confirmer('c')" value="Result">
</form>
The user should be able to choose between a, b or c. Sometimes it's only a and b (the confirmer-functions-calls are written with PHP dynamically).
The problem is: Even if the user chooses an option it calls the following function but it should break/stop/end and not asking for another confirm.
You can simplify your code a bit so that you only have to make a single function call. You can achieve this by hard-coding your options into your function, instead of hard-coding them in the inline HTML. For example:
function confirmer() {
console.clear();
var options = ['a', 'b', 'c'];
var option;
while (options.length) {
option = options.shift();
if (confirm(`Do you want ${option}?`)) {
console.log(`User wants ${option}`);
return true;
}
}
console.log('User does not want any option');
return false;
}
<input type="button" onclick="confirmer()" value="Result">
You need to check the result of each method call in order to decide if you should move on to the next one.
The following is a version of your code that will do just this.
<input type="button" onclick="if (!confirmer('a')) { if (!confirmer('b')) { confirmer('c'); } }" value="Result">
If you cannot modify the output from the PHP, then you can use a global variable to prevent the other confirmations from executing.
var confirmed = false; // global variable
function confirmer(what) {
if (!confirmed) {
var confirmed = confirm("Do you want "+what+"?");
if (confirmed) {
document.write ("User wants "+what);
}
}
}
Though this would require you to reset that global variable in order to run the code again without a page refresh. So your input might look something like this:
<input type="button" onclick="confirmer('a'); confirmer('b'); confirmer('c'); confirmed=false;" value="Result">
Notice that confirmed=false; was added at the end of the code generated by PHP.

Function results only lasting a millisecond

Im just getting into javascript and Im writing a script thats supposed to hide a submit button and show a similiar button which shows the site is processing the info. The fucntion below gets the result im looking for but only for a millisecond before returning to normal. Im not sure if the fuction is incorrect or if I have to add a sleep command or something.
<form>
<input placeholder="ID"><br>
<button id="submit" onclick="send()" type="">Submit</button>
<button id="loading"><i class="fa fa-circle-o-notch fa-spin"></i> Submiting</button>
</form>
<script type="text/javascript">
function send() {
var submit = document.getElementById('submit');
var loading = document.getElementById('loading');
if (submit.style.display === "none") {
loading.style.display = "block";
} else {
submit.style.display = "none";
loading.style.display = "block";
}
}
Your "Submit" button submits the form, which requests it again and renders the original version. To prevent this first add a parameter to your send() function which holds the data above the event:
function send(event)
Now add the following line at the end of your function:
event.preventDefault()
This prevents the form from being submitted.

how to compare textedit with javascript before action submit on yii 1?

I want run JavaScript check function, here is my code:
<script>
function check(){
var all_problem = parseInt(document.getElementById('all_problem').value);
var problem_one = parseInt(document.getElementById('problem_one').value);
var problem_two = parseInt(document.getElementById('problem_two').value);
var join_problem = problem_one+problem_two;
if (all_problem<join_problem){
//submit button is not permit to upload data
} else {
// submit button permit to upload data
}
}
</script>
And here is my submit button function:
<?php
echo CHtml::submitButton($model->isNewRecord ? 'Simpan' : 'Simpan', array('name'=>'save'));
?>
My scenario is: if all problems 5, and join problems is 6 (all problems < join problems), action on submit button won't run. and if all problems 5 and join problems 4 (all problems > join problem), action submit button will run.
Also, you should use class in the rows instead of id.
$('.problem_one').blur(function () {
var sum = 0;
$('.problem_one').each(function() {
sum += Number($(this).val());
});
// here, you have your sum
});​​​​​​​​​
Change your submit button line to this.
echo CHtml::submitButton('Simpan', array('name'=>'save','onclick'=>'return check()'));
This will help you.

validation on postback if gridview is empty

I am trying to provide an ok cancel popup 'if' the gridview has no rows and I thought this would work like a client script, but when I hit ok the control skips rest of the method. Is there anyway I could perform this validation or alert and move the control to the next statement?
protected void btnRunD_Click(object sender, EventArgs e)
{
try
{
int iESStatus = 0;
int iRunStatus = 0;
ApplicationUser au = (ApplicationUser)Session["ApplicationUser"];
if (UploadFileGrid.Rows.Count <= 0)
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "err_msg", "confirm('No new data files have been uploaded. Are you sure you want to run EYDS Processing Module?');",
//true);
}
if (au.RoleID != 2) //RoleID is not Admin!
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "err_msg", "alert('You do not have enough privileges to use this functionality!');",
true);
}
else
{.........}
}
The part I'm having difficulty with is here:
I am trying to provide an ok cancel popup 'if' the gridview has no rows
if (UploadFileGrid.Rows.Count <= 0)
{
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "err_msg", "confirm('No new data files have been uploaded. Are you sure you want to run?');",
}
Supposing your button is something like this:
<asp:Button ID="btnDoSomething" runat="server" Text="Do Something" OnClientClick="return GetConfirmation();" OnClick="btnDoSomething_Click" />
then add a javascript function as below:
function GetConfirmation() {
var rowscount = document.getElementByID(<%=UploadFileGrid.ClientID%>).rows.length;
return rowscount > 0 || confirm('No new data files have been uploaded. Are you sure you want to run?');
}
then it'll be postbacked just when the user press ok when confirm button is shown.
You can do it using javascript or jquery:
$("#<%=btnApprove.ClientID %>").click(function () {
if ($("#<%=UploadFileGrid.ClientID %> ").find("tr").length == 0) {
if (confirm('No new data files have been uploaded. Are you sure you want to run?')) {
return true;
}
else {
return false;
}
}
});
and when user press OK,your page will be postbacked,you can write the code which you want to execute after that.
I hope this code will help you.

Categories

Resources