locked
getting selected checkboxlist items values inside gridview using javascript RRS feed

  • Question

  • User1312823613 posted

    Hello,

    here is my code:

    <%@ Page Language="VB" MasterPageFile="~/SwapnaMaster.master" AutoEventWireup="false" CodeFile="SwapnaTestPage.aspx.vb" Inherits="SwapnaTestPage" title="Untitled Page"  %>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <script type="text/javascript" language="javascript">    
    <!--
    
    function ValidateASPControls()
        {
         var gvIndustryRegion=document.getElementById('<%=gvIndustryRegion.ClientID %>');
            var oArrIndustryR = gvIndustryRegion.getElementsByTagName('input'); 
            var oIndustryRLabel = gvIndustryRegion.getElementsByTagName('label'); 
            var oIndustryvalue=""
            var checkIndustrymatch=false;
            
            for(p = 0; p < oArrIndustryR.length; p++)    
            {        
                if (oArrIndustryR[p].checked)        
                {       
                alert(oIndustryRLabel[p].value)       
                }       
            } 
            }
            // -->  
        </script>
    <table>
        <tr>
            <td>
                <asp:GridView ID="gvIndustryRegion" runat="server" AutoGenerateColumns="False" Width="552px">
                    <Columns>
                        <asp:TemplateField>
                        <ItemTemplate>
                            <table>
                            <tr>
                                <td><b><%#Eval("RegionName")%></b>
                                <asp:HiddenField ID="hdnIndustryRegionId" runat="server" Value='<%#Eval("RegionID") %>' />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:CheckBoxList ID="CBLIndustryRegion" runat="server" RepeatColumns="5">                                
                                    </asp:CheckBoxList>
                                </td>
                            </tr>
                            </table>
                        </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>            
                </asp:GridView>
                </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnSave" runat="server" OnClientClick="return ValidateASPControls()" Text="Save" /></td>
        </tr>
    </table>
    </asp:Content>
    


    I want to get all the checked values from the gridview's checkboxlist. Please tell me how to achieve this. I'm using vb.net 2.0


    Wednesday, June 24, 2009 5:33 AM

Answers

  • User798903548 posted

    I want to get all the checked values from the gridview's checkboxlist. Please tell me how to achieve this. I'm using vb.net 2.0
     

    Try this:

    <script type="text/javascript">
    <!--
    function readGridCheckBoxes()  
    {  
     var gridRef = document.getElementById('<%= GridView1.ClientID %>');  
     var inputElementArray = gridRef.getElementsByTagName('input');   

     var checkedValues = '';

     for(var i=0; i<inputElementArray.length; i++)      
     {
      var elementRef = inputElementArray[i];

      if ( (elementRef.type == 'checkbox') && (elementRef.checked == true) )
      {
       var labelArray = elementRef.parentNode.getElementsByTagName('label');

       if ( labelArray.length > 0 )
       {
        if ( checkedValues.length > 0 )
         checkedValues += ', ';

        checkedValues += labelArray[0].innerHTML;
       }
      }
     }

     alert('checkedValues: ' + checkedValues);

     return checkedValues;
    }  
    // -->
    </script>

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 24, 2009 8:14 AM

All replies

  • User975061707 posted
    I am not much in to VB so I can explain the basic logic which you can implement using VB. foreach(GridViewrows gvRows in gvIndustryRegion.Rows) { if(gvRows.FindControl("CBLIndustryRegion")!=null) { CheckBoxList chkList= gvRows.FindControl("CBLIndustryRegion"); foreach(ListItem checkBox in chkList.Items) { //Here u ll get control of all the checkboxes.. } } }
    Wednesday, June 24, 2009 8:01 AM
  • User798903548 posted

    I want to get all the checked values from the gridview's checkboxlist. Please tell me how to achieve this. I'm using vb.net 2.0
     

    Try this:

    <script type="text/javascript">
    <!--
    function readGridCheckBoxes()  
    {  
     var gridRef = document.getElementById('<%= GridView1.ClientID %>');  
     var inputElementArray = gridRef.getElementsByTagName('input');   

     var checkedValues = '';

     for(var i=0; i<inputElementArray.length; i++)      
     {
      var elementRef = inputElementArray[i];

      if ( (elementRef.type == 'checkbox') && (elementRef.checked == true) )
      {
       var labelArray = elementRef.parentNode.getElementsByTagName('label');

       if ( labelArray.length > 0 )
       {
        if ( checkedValues.length > 0 )
         checkedValues += ', ';

        checkedValues += labelArray[0].innerHTML;
       }
      }
     }

     alert('checkedValues: ' + checkedValues);

     return checkedValues;
    }  
    // -->
    </script>

    NC...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 24, 2009 8:14 AM
  • User1242141900 posted

    Hi NC,


    In the page I'm developing  I needed to iterate through a CheckBoxList and check if the current item ended with a specific word; if so, had to check the current checkbox; else; uncheck it. It was a kind of Select All/None, but I have to check only the items ending with a specific word.

    I used the code you posted and made some changes to adjust to my situation and it's working perfectly!

    Thanks a lot for you post! =)


    Ana

    Friday, August 21, 2009 2:16 PM