4

I have two files named as TimeSheet.aspx.cs and TimSheet.aspx ,code of the file are given below for your reference.

when i build the application im getting error "The name 'GridView1' does not exist in the current context" even thought i have a control with the id GridView1 and i have added the runat="server" as well.

Im not able to figure out what is causing this issue.Can any one figure whats happen here.

Thanks & Regards,

=======================================
TimeSheet.aspx.cs 
=======================================
#region Using directives
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSMS.Web.UI;
#endregion

public partial class TimeSheets: Page
{   
    protected void Page_Load(object sender, EventArgs e)
    {

        FormUtil.RedirectAfterUpdate(GridView1, "TimeSheets.aspx?page={0}");
        FormUtil.SetPageIndex(GridView1, "page");
        FormUtil.SetDefaultButton((Button)GridViewSearchPanel1.FindControl("cmdSearch"));
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string urlParams = string.Format("TimeSheetId={0}", GridView1.SelectedDataKey.Values[0]);
        Response.Redirect("TimeSheetsEdit.aspx?" + urlParams, true);
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {


    }
}
=======================================================
TimeSheet.aspx
=======================================================
<%@ Page Language="C#" Theme="Default" MasterPageFile="~/MasterPages/admin.master" AutoEventWireup="true"  CodeFile="TimeSheets.aspx.cs" Inherits="TimeSheets" Title="TimeSheets List" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">Time Sheets List</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <data:GridViewSearchPanel ID="GridViewSearchPanel1" runat="server" GridViewControlID="GridView1" PersistenceMethod="Session" />
        <br />
        <data:EntityGridView ID="GridView1" runat="server"          
                AutoGenerateColumns="False"                 
                OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
                DataSourceID="TimeSheetsDataSource"
                DataKeyNames="TimeSheetId"
                AllowMultiColumnSorting="false"
                DefaultSortColumnName="" 
                DefaultSortDirection="Ascending"    
                ExcelExportFileName="Export_TimeSheets.xls" onrowcommand="GridView1_RowCommand"         
            >
            <Columns>
                <asp:CommandField ShowSelectButton="True" ShowEditButton="True" />              
                <asp:BoundField DataField="TimeSheetId" HeaderText="Time Sheet Id" SortExpression="[TimeSheetID]" ReadOnly="True" />
                <asp:BoundField DataField="TimeSheetTitle" HeaderText="Time Sheet Title" SortExpression="[TimeSheetTitle]"  />
                <asp:BoundField DataField="StartDate" DataFormatString="{0:d}" HtmlEncode="False" HeaderText="Start Date" SortExpression="[StartDate]"  />
                <asp:BoundField DataField="EndDate" DataFormatString="{0:d}" HtmlEncode="False" HeaderText="End Date" SortExpression="[EndDate]"  />
                <asp:BoundField DataField="DateOfCreation" DataFormatString="{0:d}" HtmlEncode="False" HeaderText="Date Of Creation" SortExpression="[DateOfCreation]"  />
                <data:BoundRadioButtonField DataField="Locked" HeaderText="Locked" SortExpression="[Locked]"  />
                <asp:BoundField DataField="ReviewedBy" HeaderText="Reviewed By" SortExpression="[ReviewedBy]"  />
                <data:HyperLinkField HeaderText="Employee Id" DataNavigateUrlFormatString="EmployeesEdit.aspx?EmployeeId={0}" DataNavigateUrlFields="EmployeeId" DataContainer="EmployeeIdSource" DataTextField="LastName" />

            </Columns>
            <EmptyDataTemplate>
                <b>No TimeSheets Found!</b>
            </EmptyDataTemplate>
        </data:EntityGridView>
        <asp:GridView ID="GridView2" runat="server">
    </asp:GridView>
        <br />
        <asp:Button runat="server" ID="btnTimeSheets" OnClientClick="javascript:location.href='TimeSheetsEdit.aspx'; return false;" Text="Add New"></asp:Button>
        <data:TimeSheetsDataSource ID="TimeSheetsDataSource" runat="server"
            SelectMethod="GetPaged"
            EnablePaging="True"
            EnableSorting="True"
            EnableDeepLoad="True"
            >
            <DeepLoadProperties Method="IncludeChildren" Recursive="False">
                <Types>
                    <data:TimeSheetsProperty Name="Employees"/> 
                    <%--<data:TimeSheetsProperty Name="TimeSheetDetailsCollection" />--%>
                </Types>
            </DeepLoadProperties>
            <Parameters>
                <data:CustomParameter Name="WhereClause" Value="" ConvertEmptyStringToNull="false" />
                <data:CustomParameter Name="OrderByClause" Value="" ConvertEmptyStringToNull="false" />
                <asp:ControlParameter Name="PageIndex" ControlID="GridView1" PropertyName="PageIndex" Type="Int32" />
                <asp:ControlParameter Name="PageSize" ControlID="GridView1" PropertyName="PageSize" Type="Int32" />
                <data:CustomParameter Name="RecordCount" Value="0" Type="Int32" />
            </Parameters>
        </data:TimeSheetsDataSource>

</asp:Content>
João Angelo
  • 51,934
  • 12
  • 129
  • 140
sameer
  • 1,495
  • 3
  • 20
  • 31

12 Answers12

6

Problem can be that GridView1 is not automatically added in designer.cs file. If that is case add it in designer manually.

eomeroff
  • 8,865
  • 28
  • 87
  • 127
2

Assuming a WebSite project verify that when building it you do not get Warnings like:

Generation of designer file failed: [Failure Reason]

It seems that you're not registering the custom control EntityGridView. See the Register directive to see how you can do it.

João Angelo
  • 51,934
  • 12
  • 129
  • 140
1

I've had this problem before when I've 'added' an existing file (.aspx + .aspx.cs) to a project and the designer file hasn't updated itself. I've tried many/all of the things written here, but I find that creating a new file, copying the code-front code in, then the code-behind and then rebuilding essentially does the trick. Yes, it's a pain in the * depending on the size of the file(s) you're working with, but this typically happens when I want to quickly test some demo code I've come across (some new project etc.) and throw it into my local VS.

41st
  • 99
  • 1
  • 12
  • In case it helps someone else, I was rebuilding solution by copying code from another sln and got this error for GridView1 in my newly created Default.aspx web form. GridView1 was created in the designer file so I removed it and typed it in manually. No love. It turns out the underscore was missing from Default in this portion of the page directive [Inherits="AppName._Default"]. Once I added the underscore GridView1 was recognized! – Doreen Nov 07 '18 at 18:50
0

copy the _.aspx file code and paste it on the _.aspx.cs page.

if it is not then import namespaces all the way top.

  1. <%@ Page Language="C#" AutoEventWireup="true" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %>
Ehsan
  • 1
0

It is nested, so some things don't happen automatically.

  1. You might have to manually add it to the designer, or else (in VB) explicitly use the handles keyword or (in C#) explicitly wire up with "+=" operator.
  2. Make sure all events or explicitly stated in the control mark-up

Since I see you list the event explicitly, I'd check the designer.

Patrick Karcher
  • 21,295
  • 5
  • 49
  • 65
  • Hi, I don't have the designer file created.how shall i create one.I got this code generated using .nettier template using codesmith code generator. – sameer Jan 15 '10 at 18:32
  • In your solution search for "'''GridView1 control.", and hopefully TimeSheets.designer.aspx.cs. Yeah, sometimes the IDE doesn't want to let you get to it easily for some reason. – Patrick Karcher Jan 15 '10 at 18:48
0

I had this same problem in Visual Studio 2010. The design.cs file was correctly generated. I closed Visual Studio, and reopened it. This resolved this issue for me (after much frustration).

Ben Liyanage
  • 3,515
  • 1
  • 17
  • 20
0

I found that there was an incorrect file reference name in the designer.cs file. It was the code filename with a 1 added to the end. I removed the 1 and the code ran without error. Prior to this I deleted and rebuilt the designer file without resolution.

0

I don't know if this will help, but I've been fighting with a similar situation.

The situation: I have included some code from the modified some of the templated asp.net web project into my project - specifically the login markup. For some reason, one of the "UserName" Textbox control refuses to be recognized in the designer. Strangely enough, the "UserNameLabel" control on the next line of markup is recognized:

<%@ Page Language="C#" AutoEventWireup="True" CodeBehind="Logon.aspx.cs" MasterPageFile="~/Site.master" Inherits="SimpleWebApp.Logon" %>

<asp:Content ID="LogonRegister" ContentPlaceHolderID="MainContent" runat="server">
     <div></div>
        <table align="center">
        <tr>
            <td valign="top">
                 *<asp:TextBox ID="TextBox1" runat="server" CssClass="textEntry"></asp:TextBox>*
                 <asp:Login ID="LoginUser" runat="server" EnableViewState="true" RenderOuterTable="false">
                    <LayoutTemplate>
                        <span class="failureNotification">
                            <asp:Literal ID="FailureText" runat="server"></asp:Literal>
                        </span>
                        <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification"/>
                        <div class="accountInfo">
                            <fieldset class="login">
                                <legend>Log In</legend>
                                <p>
                                    **<asp:Label ID="lblUserNameLabel" runat="server">Username:</asp:Label>**
                                    ***<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>***
                                </p>
...

What I've tried: I've restarted VWD 2010, deleted the designer file and recreated it through converting the page to a web application changed the name of the Textbox, deleted and recreated the Textbox.

I decided to experiment a little, and discovered that adding a typical textbox just outside the asp:Login tag is recognized, while adding it just inside that tag leaves it unrecognized in the designer.

Figured this might help myself or someone else to piece together what might be going on.

Anyone have any idea what might cause this behavior around the asp:Login tag?

Thotman
  • 131
  • 1
  • 2
0

For closely related answers, visit a slightly similar question, How do you force Visual Studio to regenerate the .designer files for aspx/ascx files?.

@sameer: I'd be interested to hear if you tried to replace CodeFile="TimeSheets.aspx.cs" with CodeBehind="TimeSheets.aspx.cs" before converting the project.

Community
  • 1
  • 1
WynandB
  • 1,267
  • 13
  • 15
0

The easiest way is to:

  1. Delete the designer file (YourFilename.aspx.designer.cs)
  2. Select the ASPX file in Solution Explorer
  3. Under Projects menu look for the item "Convert to Web Application" and click on it. Click Yes on the dialog window.
  4. Open up the newly created designer file and make sure the namespace matches the namespace on the primary aspx.cs file (VERY IMPORTANT)
  5. Close the designer file.
  6. Save the solution. Close it and then reopen.
Aamir
  • 680
  • 3
  • 14
  • 25
0

I know this is an old thread, but I just struggled with an issue similar to this. After a couple of days, I figured out that I had a backup copy of the problem page and code behind in my project. I kept getting the error "does not exist in the current context" at compile time on objects placed on the new page, even though intellisense recognized them. I guess VS was getting confused because of the duplicate pages in the default namespace. Once I got rid of the backup, I magically stopped getting the errors.

user735232
  • 181
  • 1
  • 5
  • This answer has guided me. I was modifying the markup of a master page that has a counterpart (with a little different markup). Both share a code behind. While modifying one of the two master pages, I experienced "...does not exist in the current context" when trying to build (even though Intellisense worked). After having modified the markup of the counterpart (in order for both pages to have same controls), the error disappeared. Fwiw, this was in a web site (not a web application). – bvgheluwe May 27 '14 at 09:25
0

I had the same problem, none of the solutions worked for me. I figured out without wasting too much time, that many of the ASPX pages in the Admin section were being given the same class name as the entities in the project entity files. Also, each of the pages listed the Entities namespace causing conflicts since after adding the entity namespace in the using directives, there were namespace conflicts. I went through and added "Page" to each of the ASPX page, recompiled and everything worked fine.

I know this thread has already been answered, but I wanted to include this description in case there are others that have this same problem and none of the above solutions worked, to give them something else that might be the issue.

I had 21 pages to change in my project, here is an example using the UserEntity and the Admin/UserEntity.aspx:

in UsersEntity.aspx front side aspx page, changed:

<%@ Page Language="C#"
         Theme="Default" 
         MasterPageFile="~/MasterPages/admin.master" 
         AutoEventWireup="true" 
         Inherits="UsersEntity" 
         Title="UsersEntity" 
         Codebehind="UsersEntity.aspx.cs" 
%>

to:

<%@ Page Language="C#"
         Theme="Default" 
         MasterPageFile="~/MasterPages/admin.master" 
         AutoEventWireup="true" 
         Inherits="UsersEntityPage" 
         Title="UsersEntity" 
         Codebehind="UsersEntity.aspx.cs" 
%>

in the UsersEntity.aspx.cs code behind, I changed:

public partial class UsersEntity : System.Web.UI.Page

to:

public partial class UsersEntityEntityPage : System.Web.UI.Page

and in the UsersEntity.aspx.designer.cs (Designer Page): That page got automatically changed when I changed the code behind page to:

public partial class UsersEntityPage {

I did that for each of the other offending pages, which were all of them except for the "Edit" pages.

-- I guess I could have just removed the using directive to the Entity name space, but I really want to be able to have access to that in my pages, plus I think it is ungood for the page classes to have the exact same name as my entity classes. It causes confusion to me to have it like that.

stephenbayer
  • 11,718
  • 15
  • 59
  • 98