Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi ..
how to hide and show table column using CheckBox if the table are in Repeater Controle
Posted
Updated 19-Sep-13 1:48am
v2
Comments
[no name] 19-Sep-13 8:46am    
What do you mean by that? elaborate

CSS
CSS
.Hide{visibility:hidden;}
        .Show {visibility:visible;}



Page

XML
<asp:ScriptManager ID="scManager" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="upTest" runat="server">
        <ContentTemplate>
             <asp:CheckBox ID="chkShowHide" runat="server" AutoPostBack="true"
                oncheckedchanged="chkShowHide_CheckedChanged" />

         <asp:Repeater ID="rptTest" runat="server">
            <ItemTemplate>
                <table id="myTable" runat="server">

                <tr id="myTr">
                     <td id="mytd1" runat="server">
                           <asp:Label ID="lblTest" runat="server" Text='<%# Eval("Col1") %>'></asp:Label>
                        </td>
                        <td id="mytd2" runat="server">
                            <asp:Label ID="lblTest2" runat="server" Text='<%# Eval("Col2") %>'></asp:Label>
                        </td>
                        <td id="mytd3" runat="server">
                             <asp:Label ID="Label3" runat="server" Text='<%# Eval("Col3") %>'></asp:Label>
                        </td>
                </tr>
                </table>

            </ItemTemplate>
        </asp:Repeater>
        </ContentTemplate>
    </asp:UpdatePanel>



CS Code

C#
foreach (Control ctrlItem in rptTest.Controls)
            {
                if (ctrlItem is RepeaterItem)
                {
                    foreach (Control ctrlTable in ctrlItem.Controls)
                    {
                        if (ctrlTable is System.Web.UI.HtmlControls.HtmlTable)
                        {
                            foreach (Control ctrlRow in ctrlTable.Controls)
                            {
                                if (ctrlRow is System.Web.UI.HtmlControls.HtmlTableRow)
                                {
                                    int i = 0;
                                    foreach (Control ctrlCell in ctrlRow.Controls)
                                    {
                                        if (ctrlCell is System.Web.UI.HtmlControls.HtmlTableCell && i <2)
                                        {
                                            if(chkShowHide.Checked)
                                            ((System.Web.UI.HtmlControls.HtmlTableCell)ctrlCell).Attributes["class"] = "Show";
                                            else
                                                ((System.Web.UI.HtmlControls.HtmlTableCell)ctrlCell).Attributes["class"] = "Hide";

                                            i++;
                                        }
                                        if (i == 2)
                                            break;
                                    }
                                }
                            }



                        }
                    }

                }


            }




Note: It is working very fine but i would suggest you to use TWO Repeaters. One with all columns and second with all - 2.
Show Repeater-1 when requred and on click of checkbox show the other one and hide the first.
(as the above solution may give you the performance hit).

Give a look to <asp:MultiView>. you may use two views in your page. and switch the selected view on check of checkbox.
 
Share this answer
 
v4
<script type="text/javascript">
        var hide1 = true;
        

        function hideColumn1(tableId, colIndex) {
            var table = document.getElementById(tableId);

            if (table != null) {
                for (i = 0; i < table.rows.length; i++) {
                    if (hide1)
                        table.rows[i].cells[colIndex - 1].style.display = 'none';
                    else
                        table.rows[i].cells[colIndex - 1].style.display = '';
                }
                hide1 = !hide1;
            }
    </script> 

<asp:repeater id="rpt" runat="server" >

       <headertemplate>
        <table  id="myTable" >
          <tr>
                <th >
                </th>
                  <th >
                </th>
                 .
                 .
                 .
        </tr>
           </table></headertemplate></asp:repeater>

<input id="Checkbox1" type="checkbox"  önclick="hideColumn1('myTable', 1)"  runat="server" checked="checked" />
<input id="Checkbox1" type="checkbox"  önclick="hideColumn1('myTable', 2)"  runat="server" checked="checked" />
<input id="Checkbox1" type="checkbox"  önclick="hideColumn1('myTable', 3)"  runat="server" checked="checked" />
.
.
.
 
Share this answer
 

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