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

[Solved] How to hide the Delete CommandItemTemplate Link buttons conditionally

2 Answers 407 Views
Grid
This is a migrated thread and some comments may be shown as answers.
gc_0620
Top achievements
Rank 1
gc_0620 asked on 27 Jan 2010, 11:29 PM
Folks

- Environment VS 2008 SP1, RadControls for ASP.NET AJAX Q3 2009 SP1,  WIN XP SP2, IE7.

I have a small Radgrid with 2 columns. I would like to know how to hide delete commanditem template link button while selecting certian rows in GridView.  For example if I am selecting  customer id's: "ANTON" or "BOLID", that delete link button should auto hide and I am selecting any other customer's that delete link button should be visibile/unhide.

Below is my code and attached is the screen printout of Grid Data Source.  Thanks

GC_0620

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
</head> 
<body> 
    <form id="form1" runat="server">  
    <div> 
      
        <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">  
        </telerik:RadScriptManager> 
      
    </div> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"   
        ConnectionString="<%$ ConnectionStrings:NwindConnectionString %>"   
        DeleteCommand="DELETE FROM [Customers] WHERE (([CustomerID] = ?) OR ([CustomerID] IS NULL AND ? IS NULL))"   
        InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName]) VALUES (?, ?)"   
        ProviderName="<%$ ConnectionStrings:NwindConnectionString.ProviderName %>"   
        SelectCommand="SELECT CustomerID, CompanyName, postalcODE FROM Customers  where  LEFT(CustomerID,2) &lt;= &quot;CB&quot; ORDER BY CustomerID"   
          
          
        UpdateCommand="UPDATE [Customers] SET [CompanyName] = ? WHERE (([CustomerID] = ?) OR ([CustomerID] IS NULL AND ? IS NULL))">  
        <DeleteParameters> 
            <asp:Parameter Name="CustomerID" Type="String" /> 
        </DeleteParameters> 
        <UpdateParameters> 
            <asp:Parameter Name="CompanyName" Type="String" /> 
            <asp:Parameter Name="CustomerID" Type="String" /> 
        </UpdateParameters> 
        <InsertParameters> 
            <asp:Parameter Name="CustomerID" Type="String" /> 
            <asp:Parameter Name="CompanyName" Type="String" /> 
        </InsertParameters> 
    </asp:SqlDataSource> 
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1"   
        GridLines="None" AllowPaging="True" AllowSorting="True">  
<MasterTableView DataSourceID="SqlDataSource1" CommandItemDisplay"TopAndBottom"    
            AutoGenerateColumns="False" DataKeyNames="CustomerID">  
<RowIndicatorColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</RowIndicatorColumn> 
 
<ExpandCollapseColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</ExpandCollapseColumn> 
    <Columns> 
        <telerik:GridBoundColumn DataField="CustomerID" DefaultInsertValue=""   
            HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID"   
            UniqueName="CustomerID">  
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="CompanyName" DefaultInsertValue=""   
            HeaderText="CompanyName" SortExpression="CompanyName" UniqueName="CompanyName">  
        </telerik:GridBoundColumn> 
    </Columns> 
<CommandItemTemplate> 
 <div style="padding: 5px 5px;">  
                        Custom command item template for Grid: &nbsp;&nbsp;&nbsp;&nbsp;  
                        <asp:linkbutton id="btnEditSelected" runat="server" CommandName="EditSelected" Visible='<%# RadGrid1.EditIndexes.Count == 0 %>'><img style="border:0px;vertical-align:middle;" alt="" src="Images/Edit.gif" /> Edit selected</asp:LinkButton>&nbsp;&nbsp;  
                        <asp:linkbutton id="btnUpdateEdited" runat="server" CommandName="UpdateEdited" Visible='<%# RadGrid1.EditIndexes.Count > 0 %>'><img style="border:0px;vertical-align:middle;" alt="" src="Images/Update.gif" /> Update</asp:LinkButton>&nbsp;&nbsp;  
                        <asp:linkbutton id="btnCancel" runat="server" CommandName="CancelAll" Visible='<%# RadGrid1.EditIndexes.Count > 0 || RadGrid1.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="Images/Cancel.gif" /> Cancel editing</asp:LinkButton>&nbsp;&nbsp;  
                        <asp:linkbutton id="LinkButton2" runat="server" CommandName="InitInsert" Visible='<%# !RadGrid1.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="Images/AddRecord.gif" /> Add new</asp:LinkButton>&nbsp;&nbsp;  
                        <asp:linkbutton id="LinkButton3" runat="server" CommandName="PerformInsert" Visible='<%# RadGrid1.MasterTableView.IsItemInserted %>'><img style="border:0px;vertical-align:middle;" alt="" src="Images/Insert.gif" /> Add this </asp:LinkButton>&nbsp;&nbsp;  
<asp:linkbutton id="LinkButton1" OnClientClick="javascript:return confirm('Delete selected?')" 
runat="server" CommandName="DeleteSelected"><img style="border:0px;vertical-align:middle;" alt="" src="Images/Delete.gif" /> Delete selected Jobs</asp:LinkButton>&nbsp;&nbsp;  
                        
                        
                    </div> 
                </CommandItemTemplate> 
</MasterTableView> 
        <ClientSettings> 
            <Selecting AllowRowSelect="True" /> 
        </ClientSettings> 
    </telerik:RadGrid> 
    </form> 
</body> 
</html> 
 

 

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 Jan 2010, 04:19 AM
Hello,

You can set the visibility of linkbutton in the SelectedIndexChanged event of grid as shown below.
CS:
 
    protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        GridDataItem item = (GridDataItem)RadGrid1.SelectedItems[0]; 
        GridCommandItem commandItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0]; 
        LinkButton lnkButton = (LinkButton)commandItem.FindControl("LinkButton1"); 
        if (item["CustomerID"].Text == "ANTON"
        { 
            lnkButton.Visible = false
        } 
        else 
        { 
            lnkButton.Visible = true
        } 
    } 

Also set the 'ClientSettings-EnablePostBackOnRowClick' property to True in order to fire SelectedIndexChanged event on selecting an item.
ASPX:
 
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" GridLines="None"
    <MasterTableView CommandItemDisplay="Top" 
        DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" ClientDataKeyNames="Number, CustomerID"
        <CommandItemTemplate> 
            <asp:LinkButton ID="LinkButton1" runat="server" Text="Delete" CommandName="DeleteSelected"></asp:LinkButton> 
        </CommandItemTemplate> 
        <Columns> 
          . . . 
        </Columns> 
    </MasterTableView> 
    <ClientSettings EnablePostBackOnRowClick="True"
        <Selecting AllowRowSelect="True" />       
    </ClientSettings> 
</telerik:RadGrid> 

Thanks,
Princy.
0
gc_0620
Top achievements
Rank 1
answered on 28 Jan 2010, 11:15 PM
Thank you Princy.

I really appreciate the contribution you, Shinu and some others are making to this community.  Thinks that takes me some hours to figure out, you and Shinu figure out them with a magic charm, like flipping a burger in the Grill !!!! 

I bookmark all of your and Shinu's posts in my Favorites. Answers to many of my questions in this learning curve can be found there.

Again appreciate for all of your help.
 
GC_0620
Tags
Grid
Asked by
gc_0620
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
gc_0620
Top achievements
Rank 1
Share this question
or