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

Error in databound checkboxlist in RadGrid EditItem Template

3 Answers 28 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 07 Oct 2013, 09:18 AM
in my radgrid of Job Title, i want to add a checkboxlist to let user check/uncheck the Department Name.
That means the job title belongs to which department.

So I want to databound all department name from table "Department". 
For New Record, it will display all department name.
In Edit Mode, if user already check before, the checkbox will automatically check. How can i use OnDataBound in code behind to databound the checkbox by RadGrid DataKeyNames "ID" ?

It returns the following error when i click Add New/Edit:

 

Server Error in '/LRDB' Application.

The DataSourceID of 'CheckBoxList_Dept' must be the ID of a control of type IDataSource. A control with ID 'LDS_Dept' could not be found.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The DataSourceID of 'CheckBoxList_Dept' must be the ID of a control of type IDataSource. A control with ID 'LDS_Dept' could not be found.

Source Error:

Line 26: <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel8" runat="server"> Line 27: </telerik:RadAjaxLoadingPanel> Line 28: <telerik:RadGrid ID="RadGrid_JobTitle" runat="server" CellSpacing="0" Line 29: DataSourceID="LDS_JobTitle" width="800" PageSize="15" Line 30: GridLines="None" style="margin-top: 0px" AllowFilteringByColumn="True" 

My code as follows:

<%@ Page Title="" Language="VB" MasterPageFile="~/LRDB.master" AutoEventWireup="false" CodeFile="Admin_dictionary_JobTitle.aspx.vb" Inherits="Admin_Admin_dictionary_JobTitle" %>
 
 
 
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
 
</asp:Content>
 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
 
        <br />
 
        <strong><span class="style4">Job Title</span><br /> </strong><br />
 
        <telerik:RadCodeBlock ID="RadCodeBlock8" runat="server">
 
  <script type="text/javascript">
 
        function RowDblClick(sender, eventArgs) {
        sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
        }
 
        function conformbox() {
        var con = confirm("Are you sure want to delete?");
        if (con == true) {
  return true;
  }
        else {
  return false;
   }
  }
  </script>
  </telerik:RadCodeBlock>
 
 
 
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel8" runat="server">
 
        </telerik:RadAjaxLoadingPanel>
 
        <telerik:RadGrid ID="RadGrid_JobTitle" runat="server" CellSpacing="0"
        DataSourceID="LDS_JobTitle" width="800"  PageSize="15"
  GridLines="None" style="margin-top: 0px" AllowFilteringByColumn="True"
        AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
        ShowStatusBar="True" AllowAutomaticDeletes="True" AllowAutomaticInserts="false"
        AllowAutomaticUpdates="True">
         <GroupingSettings CaseSensitive="false" />
 
    <MasterTableView AutoGenerateColumns="False"

     DataKeyNames="ID" DataSourceID="LDS_JobTitle"

     AllowFilteringByColumn="False" AllowPaging="False"

     CommandItemDisplay="Top" OverrideDataSourceControlSorting="true" >

      <CommandItemSettings ExportToPdfText="Export to PDF" />

      <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column"
       Visible="True">

      </RowIndicatorColumn>

      <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"
       Visible="True">

      </ExpandCollapseColumn>

      <Columns>

       <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" HeaderStyle-Width="50">

       </telerik:GridEditCommandColumn>

       <telerik:GridBoundColumn DataField="ID"
        FilterControlAltText="Filter ID column" HeaderText="ID"
        SortExpression="ID" UniqueName="ID" Visible="false">

       </telerik:GridBoundColumn>

       <telerik:GridBoundColumn DataField="Job_Title_ShortName"

        FilterControlAltText="Filter Job_Title_ShortName column" HeaderText="Short Name"

        SortExpression="Job_Title_ShortName" UniqueName="Job_Title_ShortName" HeaderStyle-Width="200">

       </telerik:GridBoundColumn>

       <telerik:GridBoundColumn DataField="Job_Title"

        FilterControlAltText="Filter Job_Title column" HeaderText="Long Name"

        SortExpression="Job_Title" UniqueName="Job_Title">

       </telerik:GridBoundColumn>

      </Columns>

      <EditFormSettings EditFormType="Template">

       <EditColumn FilterControlAltText="Filter EditCommandColumn1 column"

        UniqueName="EditCommandColumn1">

       </EditColumn>

      <FormTemplate>

 

   <table id="Table2" cellspacing="2" cellpadding="1" width="50%" border="1" rules="none"

    style="border-collapse: collapse;">

    <tr class="EditFormHeader">

    <td style="width:150px">

    <asp:Label ID="lb" Text="Short Name" runat="server"></asp:Label>                                                           

    </td>

    <td>

    <asp:TextBox ID="tb_job_title_ShortName" runat="server" Text='<%# Bind("job_title_ShortName") %>' TabIndex="2"></asp:TextBox>                                                           

    </td>

    </tr>

    <tr >

    <td >

    <asp:Label ID="Label2" Text="Long Name" runat="server"></asp:Label>                                                           

    </td>

    <td>

    <asp:TextBox ID="tb_job_title" runat="server" Text='<%# Bind("job_title") %>' TabIndex="2"></asp:TextBox>                                                           

    </td>

    </tr>

    <tr >

    <td >

    <asp:Label ID="Label1" Text="Department" runat="server"></asp:Label>                                                           

    </td>

    <td>

     <asp:CheckBoxList ID="CheckBoxList_Dept" runat="server"

      AutoPostBack="True"                                                       

      OnDataBound="CheckBoxList_Dept_DataBound" DataSourceID="LDS_Dept"

      DataTextField="Department" DataValueField="DeptID"  TabIndex="1"

      RepeatColumns="8" >

     </asp:CheckBoxList>

     <asp:LinqDataSource ID="LDS_Dept" runat="server"

     ContextTypeName="dcLRDBDataContext" EntityTypeName=""                                                                

     TableName="db_Departments">

     </asp:LinqDataSource>

    </td>

    </tr>

 

 

       <tr>

     <td align="right" colspan="2">

      <asp:Button ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>'

       runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>' >

      </asp:Button>

      <asp:Button ID="btnDelete" Text="Delete" runat="server" CausesValidation="False"

       CommandName="Delete" OnClientClick="return conformbox();"   ></asp:Button>

      <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"

       CommandName="Cancel"></asp:Button>

     </td>

    </tr>

   </table>

  </FormTemplate>

 </EditFormSettings>

      <PagerStyle PageSizeControlType="RadComboBox" />

     </MasterTableView>

     <PagerStyle PageSizeControlType="RadComboBox" />

     <FilterMenu EnableImageSprites="False">

     </FilterMenu>

 

   </telerik:RadGrid>

                 <asp:LinqDataSource ID="LDS_JobTitle"

  runat="server" ContextTypeName="dcLRDBDataContext" EntityTypeName=""

  TableName="db_Dictionary_Job_Titles">

 </asp:LinqDataSource>
 
 
 
</asp:Content>


Code Behind:

Imports System
Imports System.IO
Imports Telerik.Web.UI
Imports System.Web.UI
Imports Telerik.Web.UI.AsyncUpload
Imports System.Runtime.Serialization.Json
Imports System.Web.Services
Imports System.Runtime.Serialization
Imports System.Collections.Generic
Imports System.Drawing
Partial Class Admin_Admin_dictionary_JobTitle
    Inherits System.Web.UI.Page
    Protected Sub RadGrid_JobTitle_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid_JobTitle.ItemCommand
        RadGrid_JobTitle.MasterTableView.ClearEditItems()
 
        If e.CommandName = RadGrid.InitInsertCommandName Then   'Click Add New Record button           
            Dim editColumn As GridEditCommandColumn = CType(RadGrid_JobTitle.MasterTableView.GetColumn("EditCommandColumn"), GridEditCommandColumn)
            editColumn.Visible = False
        ElseIf (e.CommandName = RadGrid.RebindGridCommandName AndAlso e.Item.OwnerTableView.IsItemInserted) Then
            e.Canceled = True
        Else
            Dim editColumn As GridEditCommandColumn = CType(RadGrid_JobTitle.MasterTableView.GetColumn("EditCommandColumn"), GridEditCommandColumn)
            If Not editColumn.Visible Then  'Click Cancel button (both Insert and Update mode)
                editColumn.Visible = True
            End If
 
            Dim txt_job_title_ShortName As TextBox = DirectCast(e.Item.FindControl("tb_job_title_ShortName"), TextBox)
            Dim txt_job_title As TextBox = DirectCast(e.Item.FindControl("tb_job_title"), TextBox)
 
            Dim dc As New dcLRDBDataContext
 
            If e.CommandName = "PerformInsert" Then
 
                Dim newRecord1 As New db_Dictionary_Job_Title
                dc.db_Dictionary_Job_Titles.InsertOnSubmit(newRecord1)
                newRecord1.Job_Title_shortname = txt_job_title_ShortName.Text
                newRecord1.Job_Title = txt_job_title.Text
 
                newRecord1.LastUpdateDate = DateTime.Now
                newRecord1.LastUpdateBy = HttpContext.Current.Session("UserID")
                dc.SubmitChanges()
 
                RadGrid_JobTitle.MasterTableView.ClearEditItems()
                RadGrid_JobTitle.DataBind()
 
            ElseIf e.CommandName = "Update" Then
                Dim lid As Integer = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("ID")
 
                Dim rec = (From p In dc.db_Dictionary_Job_Titles Where p.ID = lid).FirstOrDefault
                If Not rec Is Nothing Then
                    rec.Job_Title_shortname = txt_job_title_ShortName.Text
                    rec.Job_Title = txt_job_title.Text
 
                    rec.LastUpdateBy = HttpContext.Current.Session("UserID")
                    rec.LastUpdateDate = DateTime.Now
                    dc.SubmitChanges()
                End If
 
                RadGrid_JobTitle.MasterTableView.ClearEditItems()
                RadGrid_JobTitle.DataBind()
            ElseIf e.CommandName = "Delete" Then
                Dim lid As Integer = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("ID")
 
                Dim chk_rec = (From a In dc.db_Competency_Lists Where a.Job_Title_ID = lid).Count
                If chk_rec > 0 Then
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "MessageBox", "alert('Cannot delete because this dictionary had been used.');", True)
                Else
 
                    Dim chk_rec2 = (From a In dc.db_Employee_Details Where a.Job_Title_ID = lid).Count
                    If chk_rec2 > 0 Then
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "MessageBox", "alert('Cannot delete because this dictionary had been used.');", True)
                    Else
 
                        Dim rec2 = (From p In dc.db_Dictionary_Job_Titles Where p.ID = lid).FirstOrDefault
                        If Not IsNothing(rec2) Then
                            dc.db_Dictionary_Job_Titles.DeleteOnSubmit(rec2)
                            dc.SubmitChanges()
                        End If
 
                    End If
 
                End If
 
 
                RadGrid_JobTitle.MasterTableView.ClearEditItems()
                RadGrid_JobTitle.DataBind()
            End If
 
            dc.Dispose()
        End If
    End Sub
 
    Protected Sub CheckBoxList_Dept_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
 
    End Sub
End Class

Thanks






3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 07 Oct 2013, 09:57 AM
Hi Joe,

To keep the default value to be checked in edit mode you can write the code in ItemDataBound event of the radgrid.

ASPX:
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" DataSourceID="LDS_Dept" DataTextField="Department" DataValueField="DeptID" TabIndex="1" RepeatColumns="8">
</asp:CheckBoxList>

VB:
Protected Sub RadGrid_JobTitle_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
        Dim edit As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        Dim check As CheckBoxList = DirectCast(edit.FindControl("CheckBoxList_Dept"), CheckBoxList)
        check.SelectedValue = DataBinder.Eval(edit.DataItem, "Department").ToString()
    End If
End Sub

To remove the error,please move the following code to below  the radgrid .

ASPX:
<telerik:RadGrid ID="RadGrid_JobTitle" runat="server" ... >
         . . . . . .
</telerik:RadGrid>
 <asp:LinqDataSource ID="LDS_Dept" runat="server" ContextTypeName="dcLRDBDataContext" EntityTypeName="" TableName="db_Departments">
 </asp:LinqDataSource>

Thanks,
Shinu


0
Joe
Top achievements
Rank 1
answered on 08 Oct 2013, 12:43 AM
thanks for your reply.

i found that it had not fired the RadGrid_JobTitle_ItemDataBound
i had added OnDataBound in checkboxlist
OnDataBound="RadGrid_JobTitle_ItemDataBound"
and amend the RadGrid_JobTitle_ItemDataBound

Protected Sub RadGrid_JobTitle_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid_JobTitle.ItemDataBound

 

        If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
            Dim edit As GridEditableItem = DirectCast(e.Item, GridEditableItem)

            Dim ID As Integer = 0
            ID = DataBinder.Eval(e.Item.DataItem, "FunctionID")
....

       End If

End Sub



but it returns error

 

Server Error in '/LRDB' Application.

Unable to cast object of type 'System.EventArgs' to type 'Telerik.Web.UI.GridItemEventArgs'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.EventArgs' to type 'Telerik.Web.UI.GridItemEventArgs'.

Source Error:

Line 90: </td> Line 91: <td> Line 92: <asp:CheckBoxList ID="CheckBoxList_Dept" runat="server" Line 93: AutoPostBack="True" Line 94: DataSourceID="LDS_Dept" 
0
Shinu
Top achievements
Rank 1
answered on 08 Oct 2013, 04:21 AM
Hi Joe,

Please try the following code snippet.

ASPX:
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" DataTextField="CustomerID" DataValueField="CustomerID" OnDataBound="CheckBoxList1_DataBound" . . . .>
</asp:CheckBoxList>

VB:
Protected Sub CheckBoxList1_DataBound(sender As Object, e As System.EventArgs)
    Dim check As CheckBoxList = DirectCast(sender, CheckBoxList)
    Dim edit As GridEditFormItem = DirectCast(check.NamingContainer, GridEditFormItem)
    check.SelectedValue = DataBinder.Eval(edit.DataItem, "CustomerID").ToString()
End Sub

Thanks,
Shinu
Tags
Grid
Asked by
Joe
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Joe
Top achievements
Rank 1
Share this question
or