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

Need to minize code in ItemDataBound method.

2 Answers 70 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Himanshu
Top achievements
Rank 1
Himanshu asked on 04 Jan 2014, 08:08 AM
I am using Telerik grid with edit form template. When ItemDataBound event fires for this gird i have written various logic which must be satisfied/set for this event. So is there any server side code which can be moved to client side or minimized by using any inbuilt feature provided by teelrik?
Mainly i am calling four methods inside this even which descriptions are:
1>SetTpcHeaderCheckBoxValues- This methos is used to set the attributes of a checkbox on grids header coloumn.
2>SetTpcControlsDefaultValueInReadMode: this method is used to set the controls in readonly mode.
3>SetTpcToolTipAndWrapping: used to set the wrap and tool tip properties.
4>SetTpcControlsDefaultValueInEditMode: used to set the controls for the grid column which is in edit mode.

I have written all details regarding to this methodas below:

Protected Sub RgThridPartyCommissionsItemDataBound(sender As Object, _
                                                        e As GridItemEventArgs)

        SetTpcHeaderCheckBoxValues(e)

        Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
        If (item IsNot Nothing) Then
            SetTpcControlsDefaultValueInReadMode(item)
            SetTpcToolTipAndWrapping(e, item)

        End If

        If e.Item.IsInEditMode Then
            SetTpcControlsDefaultValueInEditMode(e)
        End If

    End Sub

Private Sub SetTpcHeaderCheckBoxValues(ByVal e As GridItemEventArgs)

        Dim headerItem As GridHeaderItem = TryCast(e.Item, GridHeaderItem)
        If headerItem IsNot Nothing Then
            Dim headerCheckBox As CheckBox = DirectCast(headerItem("ClientSelectColumn").Controls(0), CheckBox)

            headerCheckBox.Attributes.Add("onclick", String.Format _
                              ("headerChecked(this,'{0}','{1}','{2}','lnkRemoveAllTpc','hdnTPCRowCount','hdnTPCRowChecked')", _
                               CacheDataSet.ThirdPartyCommissions, _
                              Master.InvoiceID.ToString(), _
                              rgThirdPartyCommissions.ClientID))

            If (Not CanDeleteRetailAgentCommissions) Then
                headerCheckBox.Enabled = False
            ElseIf hdnIsHeaderChecked.Value.Equals("false") Then
                hdnHeaderChbxId.Value = headerCheckBox.ClientID
            End If
        End If
    End Sub

Private Sub SetTpcControlsDefaultValueInReadMode(ByVal item As GridDataItem)

        Dim rowID = DirectCast(item.FindControl("lblRowID"), Label).Text
        Dim tpcRow = (From tpc In ThirdPartyCommissionRows
                Where (tpc.RowID = rowID AndAlso tpc.IsSelected = "True")
                Select tpc).FirstOrDefault()
        'Edit template Start
        Dim checkBox As CheckBox = DirectCast(item("ClientSelectColumn").Controls(0), CheckBox)
        checkBox.Enabled = CanDeleteRetailAgentCommissions
        checkBox.Attributes.Add("onclick", "chkClick(this,'lnkRemoveAllTpc','hdnTPCRowCount','hdnTPCRowChecked')")


        'used to maintain the state of the checkbox.
        If tpcRow IsNot Nothing Then
            item.Selected = tpcRow.IsSelected
        End If

        Dim imgBtnEdit As ImageButton = DirectCast(item.FindControl("imgbtnTpcEdit"), ImageButton)
        Dim imgBtnCopy As ImageButton = DirectCast(item.FindControl("imgbtnTpcCopy"), ImageButton)
        Dim imgBtnDelete As ImageButton = DirectCast(item.FindControl("imgbtnTpcDelete"), ImageButton)

        imgBtnEdit.OnClientClick = _
            String.Format("return onThirdPartyCommissionEditClick({0})", item.ItemIndex)
        imgBtnCopy.OnClientClick = _
            String.Format("return CanEditGrid('{0}','TPC','')", item.ItemIndex)
        imgBtnDelete.OnClientClick = _
            String.Format("return CanEditGrid('{0}','TPC','Remove')", item.ItemIndex)

        Dim lbTypeTpc As Label
        lbTypeTpc = DirectCast(item.FindControl("lbType"), Label)
        Dim lbFeeCode As Label = DirectCast(item.FindControl("lbFeeCode"), Label)

        'Fix start for defect 7877
        Dim fee As String
        If lbFeeCode IsNot Nothing Then
            fee = lbFeeCode.Text
            If (fee.Contains("Default1")) Then
                lbFeeCode.Text = fee
            End If

            If (fee.Contains("Default")) Then
                fee = fee.Replace("Default", "")
                lbFeeCode.Text = fee
            End If

            If (fee = "1") Then
                fee = fee.Replace("1", "")
                lbFeeCode.Text = fee
            End If
        End If
        'Fix end for defect 7877

        Dim radToolTipTpcDescription As RadToolTip = _
                DirectCast(item.FindControl("radToolTipTPCDescription"), RadToolTip)
        Dim radToolTipTpcCommissionOnType As RadToolTip = _
                DirectCast(item.FindControl("radToolTipTPCCommissionOnType"), RadToolTip)
        Dim type As String
        If lbTypeTpc IsNot Nothing Then
            type = lbTypeTpc.Text
            If type.Contains("/") Then
                lbTypeTpc.Text = type.Split("/")(0)
                lbFeeCode.Text = type.Split("/")(1)
                radToolTipTpcDescription.Text = type.Split("/")(1)
                radToolTipTpcCommissionOnType.Text = type.Split("/")(0)
            End If
        End If

        CheckRightsForGridControl _
            (CanAddRetailAgentCommission, CanEditRetailAgentCommission, _
             CanViewRetailAgentCommissions, CanDeleteRetailAgentCommissions, _
             imgBtnEdit, imgBtnCopy, imgBtnDelete)

        DisableLinksForHistory(imgBtnEdit, imgBtnCopy, imgBtnDelete)
    End Sub

Private Sub SetTpcToolTipAndWrapping(ByVal e As GridItemEventArgs, ByVal item As GridDataItem)

        '0th index column is hide so this attributes not required
        'Tpc grid columns count to apply wrap unwrap - 7
        For col As Integer = 1 To 7
            item(rgThirdPartyCommissions.Columns(col).UniqueName).Wrap = checkWrapPremium.Checked
        Next

        If (checkWrapPremium.Checked AndAlso e.Item.IsInEditMode = False) Then
            Dim radToolTip As RadToolTip

            radToolTip = DirectCast(item.FindControl("radToolTipTPCDescription"), RadToolTip)
            radToolTip.Visible = False

            radToolTip = DirectCast(item.FindControl("radToolTipTPCLOB"), RadToolTip)
            radToolTip.Visible = False

            radToolTip = DirectCast(item.FindControl("radToolTipTPCName"), RadToolTip)
            radToolTip.Visible = False

            radToolTip = DirectCast(item.FindControl("radToolTipTPCGroupType"), RadToolTip)
            radToolTip.Visible = False

            radToolTip = DirectCast(item.FindControl("radToolTipTPCBilling"), RadToolTip)
            radToolTip.Visible = False

            radToolTip = DirectCast(item.FindControl("radToolTipTPCCommissionOnType"), RadToolTip)
            radToolTip.Visible = False

            radToolTip = DirectCast(item.FindControl("radToolTipTPCRateType"), RadToolTip)
            radToolTip.Visible = False
        End If
    End Sub

Private Sub SetTpcControlsDefaultValueInEditMode(ByVal e As GridItemEventArgs)

        Dim itemEditForm As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
        Dim ddlTpcFeeCode As RadComboBox = _
                DirectCast(itemEditForm.FindControl("rdcmbTPCFeeCode"), RadComboBox)
        Dim ddlTpcInvoiceItemLevel As RadComboBox = _
                DirectCast(itemEditForm.FindControl("rdcmbTPCInvoiceItemLevel"), RadComboBox)
        Dim ddlTpcName As RadComboBox = _
                DirectCast(itemEditForm.FindControl("rdcmbTPCName"), RadComboBox)
        Dim ddlTpcRole As RadComboBox = _
                DirectCast(itemEditForm.FindControl("rdcmbTPCRole"), RadComboBox)
        Dim ddlTpcType As RadComboBox = _
                DirectCast(itemEditForm.FindControl("rdcmbTPCType"), RadComboBox)
        Dim ddlTpcRateType As RadComboBox = _
                DirectCast(itemEditForm.FindControl("rdcmbTPCRateType"), RadComboBox)
        Dim tbTpcRate As RadNumericTextBox = _
                DirectCast(itemEditForm.FindControl("rdtbTPCRate"), RadNumericTextBox)
        Dim tbTpcCommissionAmtCal As RadNumericTextBox = _
                DirectCast(itemEditForm.FindControl("rdtbTPCCommissionAmount"), RadNumericTextBox)
        Dim tbTpcBilling As RadTextBox = _
                DirectCast(itemEditForm.FindControl("rtbTCPBilling"), RadTextBox)

        Dim itemEdit As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        Dim btnCancelOrDelete As Button = DirectCast(itemEdit.FindControl("btnCancel"), Button)

        If ViewState("IsNewRowAdded") = True Then
            btnCancelOrDelete.CommandName = "Delete"
            btnCancelOrDelete.Attributes.Add("OnClick", "DeleteRecord('TPC');")
        Else
            btnCancelOrDelete.CommandName = "Cancel"
            btnCancelOrDelete.Attributes.Add("OnClick", "resetEditIndex('TPC');")
        End If

        Dim tpcEditRowFilter = ThirdPartyCommissionRows.ElementAt(itemEditForm.DataSetIndex)

        EnableDisablePaging("TPCClassToHidePageSelector", "TPCClassToHidePageResizer", "true")

        'Assign the default values to the respective controls
        If Not (TypeOf e.Item Is IGridInsertItem) Then

            If TypeOf e.Item Is GridEditFormItem Then

                BindTpcNameCombo(ddlTpcName)

                ddlTpcName.SelectedValue = tpcEditRowFilter.MemberID

                BindTpcRoleCombo(ddlTpcRole)
                SetTpcRoleComboAsPerPartyType(ddlTpcName, ddlTpcRole)

                SetTpcBilling(tbTpcBilling, ddlTpcName)

                BindTpcTypeCombo(ddlTpcType)
                ddlTpcType.SelectedValue = tpcEditRowFilter.CommissionOnTypeID

                BindTpcInvoiceItemLevel(ddlTpcInvoiceItemLevel, _
                                        tpcEditRowFilter.CommissionOnTypeID)
                ddlTpcInvoiceItemLevel.SelectedValue = tpcEditRowFilter.InvoiceItemID

                BindTpcFeeCode(itemEditForm)

                If (Not tpcEditRowFilter.IsInvoiceItemNonPremiumFeeIDNull()) Then
                    ddlTpcFeeCode.SelectedValue = tpcEditRowFilter.InvoiceItemNonPremiumFeeID
                Else
                    ddlTpcFeeCode.SelectedValue = 0
                End If

                If (ddlTpcFeeCode.SelectedValue <> 0) Then
                    ddlTpcFeeCode.Enabled = True
                Else
                    ddlTpcFeeCode.Enabled = False
                End If

                tbTpcBilling.Text = tpcEditRowFilter.glAgencyBillRetAgentReceivableMethodText

                BindTpcRateType(ddlTpcType, ddlTpcInvoiceItemLevel, _
                                ddlTpcFeeCode, ddlTpcRateType)
                ddlTpcRateType.SelectedValue = tpcEditRowFilter.RateTypeID
                ddlTpcRateType.Focus()

                tbTpcRate.Text = PolicyInfoCommon.FormatRate(tpcEditRowFilter.Rate, _
                                                             tpcEditRowFilter.RateTypeID)

                'Set value for hdnPremiumFeeAmtForTpc
                ddlTpcRateType.Text = ddlTpcRateType.SelectedItem.Text
                CalculateTpcCommissionAmount(ddlTpcRateType)

                tbTpcCommissionAmtCal.Text = tpcEditRowFilter.AmountDueUI

                tbTpcRate.Attributes.Add _
                    ("onBlur", "return validateThirdPartyRate('" + tbTpcRate.ClientID + "','" _
                               + tbTpcCommissionAmtCal.ClientID + "' ,'" + ddlTpcRateType.ClientID + "')")

                'If record is new then enabled required dropdowns
                If (IsNewTpcRecord = True) Then
                    ddlTpcName.Focus()

                    ddlTpcType.Enabled = True
                    ddlTpcName.Enabled = True
                    ddlTpcInvoiceItemLevel.Enabled = True

                    ddlTpcInvoiceItemLevel.Items.Clear()
                    ddlTpcInvoiceItemLevel.Items.Insert _
                        (0, New RadComboBoxItem(PolicyInfoConstants.pleaseSelect, _
                                                ColumnnIndexes.Zero))
                    ddlTpcInvoiceItemLevel.SelectedValue = 0

                    ddlTpcRateType.Items.Clear()
                    ddlTpcRateType.Items.Insert(0, New RadComboBoxItem _
                                                   (PolicyInfoConstants.pleaseSelect, _
                                                    ColumnnIndexes.Zero))
                    ddlTpcRateType.SelectedValue = 0

                    ddlTpcFeeCode.Items.Insert(0, New RadComboBoxItem _
                                                  (PolicyInfoConstants.pleaseSelect, _
                                                   ColumnnIndexes.Zero))
                    ddlTpcFeeCode.SelectedValue = 0

                    ddlTpcType.SelectedValue = 0
                    ddlTpcRole.SelectedValue = 0
                    tbTpcBilling.Text = String.Empty

                    IsNewTpcRecord = False
                End If

                If (tbTpcCommissionAmtCal.Text.Equals("")) Then
                    tbTpcCommissionAmtCal.Text = 0
                End If

                If (IsClickOnCopy) Then
                    ddlTpcInvoiceItemLevel.Enabled = True
                    ddlTpcName.Enabled = True
                    ddlTpcType.Enabled = True
                    IsClickOnCopy = False
                End If

                If ddlTpcRateType.Text = PolicyInfoConstants.flat Then
                    tbTpcRate.NumberFormat.DecimalDigits = 2
                    tbTpcCommissionAmtCal.Text = FormatNumber _
                        (tbTpcCommissionAmtCal.Text, 2)
                Else
                    tbTpcRate.NumberFormat.DecimalDigits = 4
                End If

            End If

        End If

        'Fix start for defect 8512
        Dim controlClientId As String = Nothing

        'If new record then set focus on 'Name'
        If (ddlTpcName.Enabled) Then '
            controlClientId = ddlTpcName.ClientID + "_Input"
        ElseIf (ddlTpcRateType.Enabled) Then
            'If in Edit mode then set focus on 'Rate Type'
            controlClientId = ddlTpcRateType.ClientID + "_Input"
        End If

        hdnRemoveTabValue.Value = controlClientId

        ScriptManager.RegisterClientScriptBlock _
            (Me, GetType(Page), "ToggleScript", "SetFocusAfterPostBack('" & controlClientId & "');", True)
    End Sub

2 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 09 Jan 2014, 07:13 AM
Hello Himanshu,

I have to say that the there is not built-in equivalent for the operations which are being executed in the OnItemDataBound event. Moreover the idea of the form template is to allow the user to customize the edit form according to it's preferences(like you have already done). That said I would suggest following the approach you have already implemented.

Regards,
Angel Petrov
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Himanshu
Top achievements
Rank 1
answered on 04 Apr 2014, 08:32 AM
thanks Angel Petrov for the suggetion.
Tags
Grid
Asked by
Himanshu
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Himanshu
Top achievements
Rank 1
Share this question
or