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

ItemDataBound - conditionally disable command link button?

4 Answers 297 Views
Grid
This is a migrated thread and some comments may be shown as answers.
matt
Top achievements
Rank 1
matt asked on 29 Jan 2013, 11:37 PM
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

4 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 1
answered on 30 Jan 2013, 03:46 AM
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.
0
matt
Top achievements
Rank 1
answered on 30 Jan 2013, 05:35 PM
boom! thanks, shinu.

matt
0
Kiran
Top achievements
Rank 2
answered on 01 Mar 2019, 02:16 PM

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

0
Marin Bratanov
Telerik team
answered on 06 Mar 2019, 08:12 AM
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.
Tags
Grid
Asked by
matt
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
matt
Top achievements
Rank 1
Kiran
Top achievements
Rank 2
Marin Bratanov
Telerik team
Share this question
or