ASP.NET, C#, JavaScript - How to call javascript function from codebehind file?

by Evgelen 9. December 2009 18:14

Example

1. Client Side


2. Codebehind file

protected void btnSubmit_Click(object sender, EventArgs e)
{
  const string script = "";
  ClientScript.RegisterClientScriptBlock(GetType(),"ClientScript", script);
}

Tags: , ,

JavaScript

How to uncheck some items in dynamically built asp.net checkbox list depends on selected item?

by Evgelen 28. September 2009 07:51

Task: You need to uncheck few items in checkbox list with multiple selection if someone selected specific item.

Example: You have four items in check box list Item 1, Item 2, Item 3 and Item 4. If someone select Item 4 it will uncheck the selected Item 1, Item 2, Item 3 and backward in case someone selected Item 1 or Item 2 or Item 3 it will uncheck Item 4.

Solution: You need to add the code right before body tag ends and change the listBoxId to the ID of your control. The function fires automatically for both Firefox and Internet Explorer browsers. 

 
(function() {

       

         if (document.getElementById('ControlId') != null) {
             var ie = (document.all) ? true : false;
             var ff = ((document.getElementById) && (!ie)) ? true : false;

             if (ie) {
                 var selectListie =
                 document.getElementById('ControlId');
                 selectListie.onchange = function() { getSelected() };
             }

             if (ff) {
                 var selectListff =
                 document.getElementById('ControlId');
                 selectListff.setAttribute('onchange', 'getSelected();');
             }
         }


         })();

         function getSelected() {             
             var selectListff = document.getElementById('ControlId');
             var selIndex = selectListff.selectedIndex;
                   if (selIndex != -1) {                       
                       var chkLista = document.getElementById("ControlId");
                       for (var i = 0; i < chkLista.length; i++) {
                           if (chkLista[0].selected || chkLista[1].selected || chkLista[2].selected) {
                               chkLista[3].selected = false
                               
                               return;
                           }
                       }
                   }

Tags: , , , ,

JavaScript

How to validate Asp.Net checkbox list with JavaScript?

by Evgelen 28. September 2009 07:46

Task: You need to validate a asp.net checkbox list with javascript.

Solution:

You need to create a asp.net custom validator and on client validation function (ClientValidationFunction="CheckExperince") call the JavaScript function below.

function CheckExperince(source, args) {
            var checkList = document.getElementById('');
            var checkListBox = checkList .getElementsByTagName("input");
            for (var i = 0; i &lt; checkListBox.length; i++) {
                if (checkListBox[i].checked) {
                    args.IsValid = true;
                    return;
                }
            }
            args.IsValid = false;
        }

Tags: , , ,

JavaScript

Get DropDownList selected value with JavaScript by ClientID

by Evgelen 28. September 2009 06:44
var TypeIndex = document.getElementById('').selectedIndex
var TypeValue = document.getElementById('').options[TypeIndex].value
if(TypeValue != ""){
    //Do Something
}

Tags: , , , , ,

JavaScript

Tag cloud