This is a migrated thread and some comments may be shown as answers.

[Solved] Global Javascript code for txtbox and chkbox template columns

3 Answers 102 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Prathibarani
Top achievements
Rank 1
Prathibarani asked on 22 Apr 2013, 07:16 PM
Hi,
On some pages, my radgrid has textboxes in template columns and in some other pages, we have checkboxes in template columns. Common requirement is upon submit (submit button) is clicked, I need to make sure that atleast 1 textbox or 1 checkbox is checked. So, I am planning to write a common code to be used for all the pages.

I am using this link javascript code to begin with:

For eg:
http://www.telerik.com/community/forums/aspnet-ajax/grid/checkbox-template-column-in-radgrid.aspx

<script type="text/javascript">
    function ClientClick(gridName, templateColumnName) {
        var count = 0;
        var grid = $find(gridName);
        var MasterTable = grid.get_masterTableView();
        for (var i = 0; i < MasterTable.get_dataItems().length; i++) {
            var gridItemElement = MasterTable.get_dataItems()[i].findElement("CheckBox1");
                    // Instead of hard codeing, I need to find what type of control is in template column 
                    // and based on that write code
            if (gridItemElement.checked) {
                count++;
                break;
            }
        }
        if (count == 0) {
            alert("Please check atleast one !");
            return false;
        }
    }
</script>

so something like this:
var inputElement = cell.getelementbytagname('INPUT')
if (inputElement.type) ==  "CheckBox"{}
elseif (inputElement.type == "text"

Summary, I need to find the type of control and then code.

Thanks,
Prathiba

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 23 Apr 2013, 05:06 AM
Hi,

One option is to access both the controls (TextBox as well as CheckBox ) in the JavaScript and check which one is null. Please check the following script.

Javascript:
<script type="text/javascript">
    function ClientClick(gridName, templateColumnName) {
        var count = 0;
        var grid = $find(gridName);
        var MasterTable = grid.get_masterTableView();
        for (var i = 0; i < MasterTable.get_dataItems().length; i++) {
            var gridItemElement = MasterTable.get_dataItems()[i].findElement("CheckBox1");
            if (gridItemElement !== null)
            {
               //do your code
            }       
             
        }
         
    }
</script>

Thanks,
Princy.
0
Prathibarani
Top achievements
Rank 1
answered on 25 Apr 2013, 03:59 PM
Thanks Princy for solution. I don't think that will work out, because what if say 2 pages has checkboxes. One page has named checkbox as 'chkAmount' and in another page it is named as 'chkRate'. So, hardcoding to the id of controls won't work. I need to know the type of the control.


I appreciate your response.
Thanks,
Prathiba.
0
Prathibarani
Top achievements
Rank 1
answered on 25 Apr 2013, 04:41 PM
I solved the problem. It is working fine. This is what I am doing.

<script type="text/javascript">
    function ClientClick(gridName, controlID) {
        var count = 0;
        var grid = $find(gridName);
        var MasterTable = grid.get_masterTableView();
        for (var i = 0; i < MasterTable.get_dataItems().length; i++) {
            var gridItemElement = MasterTable.get_dataItems()[i].findElement(controlID);
            if (gridItemElement !== null)
                    var elementType = gridItemElement.type;

                 switch(elementType){
                    case "checkbox":
                          //do your code
                          break;
                    case "text":
                          // do your code
                          break;
                  }
                                
             
        }
         
    }
</script>

Tags
Grid
Asked by
Prathibarani
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Prathibarani
Top achievements
Rank 1
Share this question
or