user1916528
user1916528

Reputation: 389

Hide a LinkButton in a FormView's ItemTemplate

I am attempting to hide the "Delete" button inside a FormView's Item Template in my ASP.net c# Web Forms application. The FormView's ID is "fvAssnmtDets" and the delete button's ID is "DeleteButton". I am unable to get it to hide, instead I get an error stating "System.NullReferenceException: 'Object reference not set to an instance of an object.'".

There is a GridView located at the top of the page, and when a row is selected information populates the FormView. I get the error when I select a row in the Gridview. I have tried the following using OnSelectedIndexChanged of the GridView:

protected void rgAssnmtList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if ((HttpContext.Current.User.IsInRole("AKOB")) && (fvAssnmtDets.CurrentMode == FormViewMode.ReadOnly))
        {
            ((LinkButton)fvAssnmtDets.FindControl("DeleteButton")).Visible = true;
        }
        else
        {
            ((LinkButton)fvAssnmtDets.FindControl("DeleteButton")).Visible = false;
        }
    }

And I also tried:

protected void rgAssnmtList_SelectedIndexChanged(object sender, EventArgs e)
    {
        LinkButton lbDelete = (LinkButton)fvAssnmtDets.FindControl("DeleteButton");

        if (HttpContext.Current.User.IsInRole("AKOB") && (fvAssnmtDets.CurrentMode == FormViewMode.ReadOnly))
        {
            lbDelete.Visible = true;
        }
        else
        {
            lbDelete.Visible = false;
        }
    }

I have also tried the above in the FormView's DataBound event, but I get the same error.

Here's the button syntax:

<asp:LinkButton runat="server" Text="Delete" CommandName="Delete" ID="DeleteButton" CausesValidation="False" CssClass="btn btn-danger" Visible="false" />.

I verified that it does work properly - when I click it the record is deleted.

I feel as though I'm getting the null exception error because the above code is running before the FormView is populated with data from the selected row of the GridView. Is this correct? If so, how do I get the data to load in the FormView first? If not, what is the solution?

Upvotes: 0

Views: 500

Answers (1)

hardkoded
hardkoded

Reputation: 21627

Make sure that you're hiding the button after the FormView is binded. You don't need to cast a Control to LinkButton to access to the Visible property.

public partial class FormViewDemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        rgAssnmtList.DataSource = new[] {
            new {Id= 1, Text = "Text 1"  },
            new {Id= 2, Text = "Text 2"  },
        };
        rgAssnmtList.DataBind();
    }

    protected void rgAssnmtList_SelectedIndexChanged(object sender, EventArgs e)
    {
        var list= new List<dynamic>() {
            new {Id= 1, Text = "Text"  },
            new {Id= 2, Text = "Text"  },
        };
        fvAssnmtDets.DataSource = list;
        fvAssnmtDets.DataBind();
        fvAssnmtDets.FindControl("DeleteButton").Visible = rgAssnmtList.SelectedDataKey.Value.ToString() == "1";
    }

}

Upvotes: 1

Related Questions