Setting Button property in a Gridview TemplateField ONLY if there is a single row
Posted on 2008-11-05
I'm using .Net 2.0 and coding in vb.net. I have a Gridview which displays an edit and a delete button for each record. I cannot allow the user to delete the row if it is the only row in the grid so I would like to set the visible property of my delete button to False if it is the only row.
Currently I am checking Grid.Rows.Count = 1 in my Grid.RowCommand event after the grid has been created and displaying a message but I would like to simply hide the delete button.
I have tried the solution below in both the RowDataBound and DataBound without success. When I write the Count back to the browser it is incrementing so that my code will execute incorrectly if there are more than 1 rows. Also the query "If e.Row.RowType = DataControlRowType.DataRow " never fires so even if the next code bit worked it wouldn't fire:
Dim DelButton As Button = CType(e.Row.FindControl("btnDelete"), Button)
DelButton.Visible = False
or, equally unsuccessful,
e.Row.FindControl("btnDelete").Visible = False
Here is the aspx part of my code:
<asp:Button ID="btnDelete" runat="server" CommandName="RemoveDetail" CausesValidation="False" Text="Del" />
I should also mention that I am using Master Pages and this code is on the content page.
Could someone get me moving in the right direction on this?