• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 471
  • Last Modified:

Setting Button property in a Gridview TemplateField ONLY if there is a single row

Hi,
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:TemplateField>
                    <ItemTemplate>
                 <asp:Button ID="btnDelete" runat="server" CommandName="RemoveDetail" CausesValidation="False" Text="Del"  />
             </ItemTemplate>
           </asp:TemplateField>    

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?

Thanks!
0
thefarm
Asked:
thefarm
  • 3
  • 3
1 Solution
 
thefarmAuthor Commented:
I also have tried setting a variable to False or True in the grid_PreRender event and then using

<asp:Button Visible = "<%# varSetInPrerender %>" ID="btnDelete" />

in my aspx code but that isn't doing the trick either although I am able to get a good grid..Rows.Count in PreRender.

Anyone?
0
 
prairiedogCommented:
After a GridView is completely bound, then you can access Grid.Row.Count. But RowDataBound fires before the GridView is completly bound, so if you try to call Grid.Row.Count in this event handler, it will return 0. The same as DataBound event handler.
However, RowCommand fires after the GridView is bound, so you can use GridView.Row.Count to get the number of the rows (if you don't have paging). If you have paging enabled, GridView.Rows.Count returns the number of rows for the current page, and it is NOT the actual total number rows of your GridView. Be careful here.
I would suggest you to get the actual total number of records from your datasource, then save it in ViewState, then in your RowDataBound or RowCreated (I prefer to use this one), you can check the ViewState, then hide/display the delete button.
 
0
 
thefarmAuthor Commented:
By putting the rowcount into the viewstate from my datasource I am able to consistently identify the rowcount value. However I am still unable to find the button using either:

Dim DelButton As Button = CType(e.Row.FindControl("btnDelete"), Button)
DelButton.Visible = False
or
e.Row.FindControl("btnDelete").Visible = False

both of these return a "Object reference not set to an instance of an object". I am using these in the OnRowCreated as PrairieDog suggested.

I believe this is because I am using Master Pages  and my routines are happening on a content page? Also the Button is in an ItemTemplate tag..not sure if that effects the discovery of the control. I'd like to understand this better so if there are any references that are good I'd appreciate knowing them.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
prairiedogCommented:
Are you saying that your GridView is on the master page, and you are accessing it from the content page?
0
 
thefarmAuthor Commented:
No my Gridview is on the content page and I'm using  the codebehind to manipulate it.

I actually got it to work by doing this in the RowCreated Method:

        If ViewState("CurrentRowCount") = 1 Then
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim DelButton As Button = CType(e.Row.FindControl("btnDelete"), Button)
                DelButton.Visible = False
            End If
        End If

I had to check first if I was on a DataRow. It works perfectly now. I guess the master page wasn't the problem afterall. It was just bombing on the header row maybe?
0
 
prairiedogCommented:
Yes, you are right. No matter what event handler you are using, RowDataBoudn or RowCreated, you will always need to check if it is a DataRow. But for RowCommand event handler, you don't need to check because only a DataRow can fire a command.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now