Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello everyone, i have a button in asp.net as follows
ASP.NET
<asp:Button ID="btnNext" runat="server" Height="26px" OnClientClick="return CheckIfOptionSelected();" OnClick="btnNext_Click" Text="Next"
                Width="77px" />


i have done some validation to radio buttons, if nothing is selected in the radio buttons then it returns false...hence the server side event does not fire. This works fine, however, i have a jquery timer...and according to the requirement...after the timer elapse i want to submit the form...or cause the onclick to fire no matter what...so i've written the following
JavaScript
function CheckIfOptionSelected() {

            var radioButton1 = document.forms[0].elements['ctl00_MainContent_rdBtnListOptions_0'];
            var radioButton2 = document.forms[0].elements['ctl00_MainContent_rdBtnListOptions_1'];
            var radioButton3 = document.forms[0].elements['ctl00_MainContent_rdBtnListOptions_2'];
            var radioButton4 = document.forms[0].elements['ctl00_MainContent_rdBtnListOptions_3'];
           
            if (radioButton1.checked != true && radioButton2.checked != true && radioButton3.checked != true && radioButton4.checked != true && countdownCurrent != 0) {

                return false;
                
            }

            else {
                countdownReset();
                return true;
            } 
        }

        function countdownReset() {
            var newCount = parseInt($('#ctl00_MainContent_example2submit').val()) * 100;
            if (newCount > 0) { countdownCurrent = newCount; }
        }


now this works fine as well...but only when nothing is selected in the radiobuttonlist...however ,if i select an item...and let the timer elapse...in this case the onclick does not fire. I don't know what the issue is?...FYI- the jquery causes the "Next" button to Click itself when the timer elapse.

the jquery i'm using...
JavaScript
var countdownTimer, countdownCurrent;
        $(document).ready(function () {
            countdownCurrent = $('#ctl00_MainContent_example2submit').val() * 100;
            countdownTimer = $.timer(function () {
                var min = parseInt(countdownCurrent / 6000);
                var sec = parseInt(countdownCurrent / 100) - (min * 60);
                var micro = pad(countdownCurrent - (sec * 100) - (min * 6000), 2);
                var output = "00"; if (min > 0) { output = pad(min, 2); }
                $('.countdowntime').html(output + ":" + pad(sec, 2) + ":" + micro);
                if (countdownCurrent == 0) {
                    var btn = document.getElementById('<%=btnNext.ClientID%>');
                    btn.click();

                } else {
                    countdownCurrent -= 7;
                    if (countdownCurrent < 0) { countdownCurrent = 0; }
                }
            }, 70, true);
            $('#example2submit').bind('keyup', function (e) { if (e.keyCode == 13) { countdownReset(); } });

        });


another thing i noticed is that the generated HTML contains onclick="return CheckIfOptionSelected();"...
HTML
<div class="ctrlStyle1">
            <span id="ctl00_MainContent_btnNext$ajaxdest" name="__ajaxmark"><input type="submit" name="ctl00$MainContent$btnNext" value="Next" onclick="return CheckIfOptionSelected();" id="ctl00_MainContent_btnNext" style="height:26px;width:77px;" /></span>   
            <span id="ctl00_MainContent_btnSubmit$ajaxdest" name="__ajaxmark"></span>
        </div>
Posted
Updated 30-Mar-13 20:15pm
v3

finally solved it...got an answer in stackoverflow...the Solution was to use UseSubmitBehaviour="false" attrib...
 
Share this answer
 
Oh I'm probably going to ripperd for this one, but I would revise your code, and test the var countdownCurrent a couple of times to make sure the value is correct, and just make a var to validate with, and return it once. I don't know what the webform looks like, so I'm guesing at the element names and the use of jquery in the bottom function.

var countdownCurrent = 0;

function CheckIfOptionSelected() {
 
  var vFlag = true;  
  var radioButton1 = $('[id*="rdBtnListOptions_1"]');
  var radioButton2 = $('[id*="rdBtnListOptions_2"]');
  var radioButton3 = $('[id*="rdBtnListOptions_3"]');
  var radioButton4 = $('[id*="rdBtnListOptions_4"]');          
  if (radioButton1.checked == false && radioButton2.checked == false && radioButton3.checked == false && radioButton4.checked == false && countdownCurrent > 0) {
 
     vFlag = false;
     alert("countdownCurrent=" + countdownCurrent);
                
  }
  else {
     countdownReset();
     alert("countdownCurrent=" + countdownCurrent);
     vFlag = true;
  } 

  return vFlag;
}
 
function countdownReset() {
  var newCount = parseInt($('[id*="example2submit"]').val()) * 100;
  if (newCount > 0) { 
    countdownCurrent = newCount; 
  }
}
 
Share this answer
 
Comments
Minghang 31-Mar-13 1:45am    
hello jkirkerx....thanks for the reply...i checked the countdowncurrent value....the value is 1000 in the else condition...and the vflag also get's set to true...so after that shouldn't the OnClick event get fired after returning true??
jkirkerx 31-Mar-13 2:14am    
Well, true will fire the button event, and false will prevent the button from firing.
So the elements are being registered, and the values are being picked up.
You might have to check the condition one more time, I'm not familiar with what your trying to do, so I'm just guessing, but add some more logic.

hmm.

else {
if (0 > parseInt($('[id*="example2submit"]').val()) * 100;){
// check condition again for true or false
}
}

return vFlag;
}

jkirkerx 1-Apr-13 13:29pm    
After I left the office that night, I realized your working on a quiz, in which there is a counter that counts down time, so you probably need 2 flags, a time flag and a validation flag, and you have to check both conditions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900