ItemDataBound - conditionally disable command link button?

5 posts, 1 answers
  1. matt
    matt avatar
    260 posts
    Member since:
    Oct 2007

    Posted 29 Jan 2013 Link to this post

    hello,

    in my grid's edit-mode i am using some conditional code to disable column editing for certain columns, like so:

    protected void gridItems_ItemDataBound(object source, GridItemEventArgs e)
    {
        if (e.Item.IsInEditMode && e.Item is GridEditableItem)
        {
            //NOTE: if this grid supported inserts, we'd have to test against "e.Item.ItemIndex == -1", since -1 is used by inserts.
     
            GridEditableItem editableItem = e.Item as GridEditableItem;
     
            //get test status status
            string status = ((TextBox)editableItem["Status"].Controls[0]).Text;
     
            switch (status.ToLower())
            {
                case "rejected":
                    editableItem["UseInAllocation"].Enabled = false;
                    editableItem["UseInToleranceCalculation"].Enabled = false;
                    editableItem["AcceptanceStatus"].Enabled = false;                      
                    break;
     
                //[set others case conditions here]
            }
        }
    }

    ...my question -- how can i similarly disable:

    a) the "Edit" command column link-button before a row enters edit-mode
    b) the "Update" command link-button while in edit-mode

    i will post the solution if i find it.

    thanks,
    matt
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 29 Jan 2013 Link to this post

    Hi,

    Please take a look into the following code snippet.

    C#:
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem ditem = (GridDataItem)e.Item;
            if(your condition)
            {
                LinkButton editbutton = (LinkButton)ditem["EditCommandColumn"].Controls[0]; //hide the EditCommandColumn
                editbutton.Visible = false;
            }
        }
        if (e.Item.IsInEditMode && e.Item is GridEditableItem)
        {
            if (!e.Item.OwnerTableView.IsItemInserted)
            {
                GridEditableItem editableItem = e.Item as GridEditableItem;
                LinkButton updateButton = (LinkButton)editableItem.FindControl("UpdateButton"); //hide the update button
                updateButton.Visible = false;
            }
        }
    }

    Thanks,
    Shinu.
  3. matt
    matt avatar
    260 posts
    Member since:
    Oct 2007

    Posted 30 Jan 2013 Link to this post

    boom! thanks, shinu.

    matt
  4. Kiran
    Kiran  avatar
    55 posts
    Member since:
    Oct 2018

    Posted 01 Mar 2019 Link to this post

    Hi,

    Is there a way I could capture Command Item like linkbutton in the ItemDataBound? I am conditionally formatting records which do meet my condition. I want to hide the Command Item button if there exists atleast one such item. 

    I tried doing below but the page throws error. 'btn' is the link button I want to Hide.

    Protected Sub rgOsd_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgOsd.ItemDataBound
            'Is it a GridDataItem
            If (TypeOf e.Item Is GridDataItem) Then
                Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
                Dim btn As LinkButton = rgOsd.MasterTableView.GetItems(GridItemType.CommandItem)(0).FindControl("LinkButton2")
                If (dataItem("SID_QTY").Text <> dataItem("LOADS_QTY").Text) Or (dataItem("SID_QTY").Text = 0 And dataItem("LOADS_QTY").Text = 0) Then
                    'dataItem.CssClass = "format"
                    dataItem.BackColor = Color.DarkRed
                    dataItem.Visible = False
                    btn.Visible = False
                End If

                Call GetUnmatchedParts()
                Call GetUnmatchedPartsfromSID()

                If lblunmatchedParts.Text <> 0 Then
                    dataItem.BackColor = Color.DarkRed
                    dataItem.Visible = False
                End If

                If lblunmatchedPartsSid.Text <> 0 Then
                    dataItem.BackColor = Color.DarkRed
                    dataItem.Visible = False
                End If



            End If

        End Sub

  5. Marin Bratanov
    Admin
    Marin Bratanov avatar
    5347 posts

    Posted 06 Mar 2019 Link to this post

    Hi Kiran,

    Assuming that you have a command item template that has a link button with ID "LinkButton2", this should work. I am attaching below a small page I built on top of this code that works fine for me so you can compare against it. Here is the relevant extract:

    Protected Sub rgOsd_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rg1.ItemDataBound
        'Is it a GridDataItem
        If (TypeOf e.Item Is GridDataItem) Then
            Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
            'add actual logic to determin whether to hide the button here, this example hides it in al lcases
            Dim btn As LinkButton = DirectCast(sender, RadGrid).MasterTableView.GetItems(GridItemType.CommandItem)(0).FindControl("LinkButton2")
            If Not Object.Equals(btn, Nothing) Then
                btn.Visible = False
            End If
        End If
    End Sub
    <telerik:RadGrid runat="server" ID="rg1" RenderMode="Lightweight" AllowPaging="true">
        <MasterTableView CommandItemDisplay="Top">
            <CommandItemTemplate>
                <asp:LinkButton Text="first button that will remain visible" ID="LinkButton1" runat="server" />
                <asp:LinkButton Text="second button we will hide" ID="LinkButton2" runat="server" />
            </CommandItemTemplate>
        </MasterTableView>
    </telerik:RadGrid>


    Regards,
    Marin Bratanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top