Search code examples
c#asp.netgridviewascx

Control does not exist in current context asp.net c#


I have two files: FileGridCS.ascx and FileGridCS.ascx.cs

Within FileGridCS.ascx: I have added the control:

<asp:GridView ID="gvFiles" runat="server"></asp:GridView>

The header of this file is:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FileGridCS.ascx.cs"
    Inherits="UserControls_FileGridCS" %>

Now, my class FileGridCS.ascx.cs is not recognising 'gvFiles'. I Get the following error:

Severity Code   Description  Project          File  Line    Suppression State
Error    CS0103 The name 'gvFiles' does not         137     Active
                exist in the current context

What am I doing wrong?, I am a little confused on why this happening when the control exists!

Thanks =)

UPDATE

Just to let you know, my class FileGrid.ascx.cs is a public partial class*


Solution

  • First You have to Specify the User Control on the .aspx Page

    In which src is Location of that User Control....

    In tagName with same name of .ascx in my case UserControl is GridControl.ascx and specify that tagName as GridControl

    And also use TagPrefix as your choice...

    <%@ Register Src="~/App_UserControls/GridControl.ascx" TagName="GridControl" TagPrefix="UC" %>
    

    Then use as GridView Like Below Example...

    <UC:GridControl ID="gvFiles" runat="server" />