Obsivus
Obsivus

Reputation: 8359

How can I hide asp controls or div tags that are inside a asp:GridView

I have this ASP:GridView and I cant seem to be able to declare the asp controls or div tags inside this ASP:GridView in my codebehind ascx file.

<asp:GridView runat="server" ID="siteMembersView" AllowPaging="True" 
            ShowHeader="False" EnableModelValidation="True" AutoGenerateColumns="false" CssClass="membership-gridview" Width="100%" CellPadding="0" CellSpacing="0">
        <Columns>
            <asp:TemplateField>
            <ItemTemplate>

             //content

            </ItemTemplate>
            </asp:TemplateField>
        </Columns>
</asp:GridView>

I am trying to put Mydiv.Visible = false and LinkButton.Visible = False But it cant find the IDs. I am using runat="server". I think the problem is that its inside the GridView beacuse I tried to put it outside the GridView and it worked perfectly.

Any kind of help is appreciated

Upvotes: 1

Views: 4438

Answers (1)

IrishChieftain
IrishChieftain

Reputation: 15253

Use the FindControl method to access your controls, then you can apply the logic to toggle the visibility of each:

How to find control in TemplateField of GridView?

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    HtmlGenericControl myDiv;
    LinkButton myLinkButton;

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        myDiv = (HtmlGenericControl)e.Row.FindControl("myDiv") 
            as HtmlGenericControl;
        myDiv.Visible = false;

        myLinkButton = (LinkButton )e.Row.FindControl("myLinkButton") 
            as LinkButton;
        myLinkButton.Visible = false;
    }
}

Upvotes: 2

Related Questions