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

GridTemplateColumn checkbox uncheck another checkbox in row

1 Answer 108 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tash
Top achievements
Rank 1
Tash asked on 09 Jul 2012, 03:10 PM
What I am trying to do is uncheck another checkbox in the same row. I want to have a subscribe and unsubscribe checkbox columns. So when you click on subscribe the unsubscribe checkbox should uncheck and vise versa. I found a lot of entries that are similar in the forums but didnt come across anything in this. Found something in the old ASP forum but its not working. Below is what I ahve.

Source
<telerik:RadGrid ID="grdPubList" runat="server" Skin="Office2007" GridLines="None"
                                               AutoGenerateColumns="False" AllowNaturalSort="false" CellSpacing="0">
                                               <HeaderStyle Width="100px" HorizontalAlign="Center" />
                                               <ClientSettings>
                                                   <Selecting CellSelectionMode="None"></Selecting>
                                               </ClientSettings>
                                               <MasterTableView AllowMultiColumnSorting="true" ShowHeadersWhenNoRecords="false">
                                                   <NoRecordsTemplate />
                                                   <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                                                   <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
                                                   </RowIndicatorColumn>
                                                   <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
                                                   </ExpandCollapseColumn>
                                                   <Columns>
                                                       <telerik:GridTemplateColumn ItemStyle-Width="250px" HeaderStyle-Width="250px" ItemStyle-HorizontalAlign="Left"
                                                           ItemStyle-VerticalAlign="Top" HeaderText="Publication Name">
                                                           <ItemTemplate>
                                                               <asp:Label ID="lblPubId" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.PubID").ToString() %>'
                                                                   CssClass="label_default_nb" Visible="false"></asp:Label>
                                                               <asp:Label ID="lblPubName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name").ToString() %>'
                                                                   CssClass="label_default_nb"></asp:Label>
                                                           </ItemTemplate>
                                                           <HeaderStyle Width="250px"></HeaderStyle>
                                                           <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="250px"></ItemStyle>
                                                       </telerik:GridTemplateColumn>
                                                       <%--<telerik:GridBoundColumn HeaderText="Practice Area" DataField="departmentDescription"
                                                           AllowSorting="false" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top"
                                                           ItemStyle-Width="100px" HeaderStyle-Width="50px">
                                                           <HeaderStyle Width="50px"></HeaderStyle>
                                                           <ItemStyle HorizontalAlign="Left" VerticalAlign="Top" Width="100px"></ItemStyle>
                                                       </telerik:GridBoundColumn>--%>
                                                       <%--<telerik:GridTemplateColumn ItemStyle-HorizontalAlign="Center" ItemStyle-Wrap="False"
                                                           ItemStyle-VerticalAlign="Top" HeaderText="Modified <br> By/Date" UniqueName="ModifiedByDate"
                                                           ItemStyle-Width="100px" HeaderStyle-Width="100px">
                                                           <ItemTemplate>
                                                               <asp:Label ID="lblUserModified" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.userModified").ToString() %>'
                                                                   CssClass="label_default_nb" Width="100px"></asp:Label>
                                                               <asp:Label ID="lblDateModified" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.dateModifiedStr").ToString() %>'
                                                                   CssClass="label_default_nb" Width="100px"></asp:Label>
                                                           </ItemTemplate>
                                                       </telerik:GridTemplateColumn>--%>
                                                       <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn1 column"
                                                           HeaderText="Format" UniqueName="typeDescription" DataField="typeDescription">
                                                           <ItemTemplate>
                                                               <asp:Label ID="lblPubType" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.typeDescription").ToString() %>'
                                                                   CssClass="label_default_nb"></asp:Label>
                                                           </ItemTemplate>
                                                       </telerik:GridTemplateColumn>
                                                       <telerik:GridTemplateColumn HeaderText="Subscribe">
                                                           <ItemTemplate>
                                                               <asp:CheckBox ID="radchkSubscribe" runat="server" />
                                                           </ItemTemplate>
                                                       </telerik:GridTemplateColumn>
                                                       <telerik:GridTemplateColumn HeaderText="Unsubscribe">
                                                           <ItemTemplate>
                                                               <asp:CheckBox ID="radchkUnsubscribe" runat="server" />
                                                           </ItemTemplate>
                                                       </telerik:GridTemplateColumn>
                                                   </Columns>
                                                   <EditFormSettings>
                                                       <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                                                       </EditColumn>
                                                   </EditFormSettings>
                                               </MasterTableView>
                                               <FilterMenu EnableImageSprites="False">
                                               </FilterMenu>
                                           </telerik:RadGrid>


VB code in the page. 
Private Sub grdPubList_ItemCreated(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles grdPubList.ItemCreated
      If TypeOf e.Item Is GridDataItem Then
          Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
          Dim chk1 As CheckBox = DirectCast(item.FindControl("radchkSubscribe"), CheckBox)
          Dim chk2 As CheckBox = DirectCast(item.FindControl("radchkUnsubscribe"), CheckBox)
          'Dim chk3 As CheckBox = DirectCast(item.FindControl("CheckBox3"), CheckBox)
          chk1.Attributes.Add("onclick", (("checkUncheck('" + chk1.ClientID & "','") + chk2.ClientID & "',')"))
           
      End If
  End Sub


Javascript in masterpage. Though i think the ch2.checked should be false in the if and true in the else.
<script type="text/javascript">
       function checkUncheck(c1, c2) {
           var ch1 = document.getElementById(c1);
           var ch2 = document.getElementById(c2);
 
           if (ch1.checked) {
               ch2.checked = true;
 
           }
           else {
               ch2.checked = false;
 
           }
       }    
   </script>

1 Answer, 1 is accepted

Sort by
0
Casey
Top achievements
Rank 1
answered on 09 Jul 2012, 03:54 PM
Hi Tash,

Have you verified that the javascript event is being hit when the checkboxes are checked/unchecked? If you don't have a preference to changing the checked state client side vs server side, you could subscribe to the OnCheckedChanged event of each checkbox and change it there.

I believe that something like the following would work.

I hope this helps!
Casey

protected void radchkSubscribe_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cbSubscribe= (CheckBox)sender;
        GridDataItem dataItem = (GridDataItem)cbSubscribe.NamingContainer;
        CheckBox cbUnsubscribe= (CheckBox)dataItem.FindControl("radchkUnsubscribe");
       if (cbSubscribe.Checked)
      {
          cbUnsubscribe.Checked = false;
      }
      else
      {
         cbUnsubscribe.Checked = true;
      }
    }

Tags
Grid
Asked by
Tash
Top achievements
Rank 1
Answers by
Casey
Top achievements
Rank 1
Share this question
or