Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LinkButton on GridView

Status
Not open for further replies.

rnooraei

Programmer
Apr 5, 2005
74
CA
Hi
I have the following code in my GridView, I would like to make my linkbutton visible or invisible (depend on action)
please see the following code

Dim CancelButton As LinkButton = CType(Me.grdContacts.FindControl("LinkButton_Cancel"), LinkButton)
Dim DeleteButton As LinkButton = CType(Me.grdContacts.FindControl("LinkButton_Delete"), LinkButton)
CancelButton.Visible = True
DeleteButton.Visible = False

but when I get to CancelButton.Visible = True , I get the following error message.
NullReferenceExeption was unhandled by user code
any help appreciated.
Thanks
 
What event is this code in? Is the FindControl() actually finding the control?
 
Assuming this is in the RowDataBound Event and assuming these controls are located within a TemplateField like this
Code:
<TemplateField>
    <ItemTemplate>
        <asp:Button ID="LinkButton_Delete" runat="server" ... />
    </ItemTemplate>
    <EditTemplate>
        <asp:Button ID="LinkButton_Cancel" runat="server" ... />
    </EditTemplate>
</TemplateField>
then
Code:
if e.Row.RowIndex > -1 Then
   Dim CancelButton As LinkButton = CType(Me.grdContacts.FindControl("LinkButton_Cancel"), LinkButton)
   If (Not CancelButton is Nothing) Then
      CancelButton.Visible = True
   End If

   Dim DeleteButton As LinkButton = CType(Me.grdContacts.FindControl("LinkButton_Delete"), LinkButton)
   If (Not DeleteButton is Nothing) Then
      DeleteButton .Visible = True
   End If
End If
when using templateFields selected controls are only rendered for a specific template (Item, Select, Edit) so depending on row state will effect which controls are available.

the RowIndex > -1 checks to make sure your not in the Header or Footer row.
you could also check the RowState and RowType if you don't want to use Indexes or Nothing checks. Just remember to Check for Item or AlternateItem if you go the non-index route.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi
What is happen if I want to use in the other event

Protected Sub grdContacts_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdContacts.RowCommand
If e.CommandName = "Edit" Then
Dim CancelButton As LinkButton = CType(Me.grdContacts.FindControl("LinkButton_Cancel"), LinkButton)
Dim DeleteButton As LinkButton = CType(Me.grdContacts.FindControl("LinkButton_Delete"), LinkButton)
CancelButton.Visible = True
DeleteButton.Visible = False
End If

End Sub
 
if you use row command you have 2 options
1. get the row index the command was issued against.
2. cycle throug each row in the grid and preform the action.

personall this is the main purpose of the RowDataBound event. I would use that instead of the RowCommand event.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top