You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET项目中CustomersGridView不存在于当前上下文的错误排查求助

ASP.NET项目中CustomersGridView不存在于当前上下文的错误排查求助

各位好,我正在做第一个ASP.NET(C#)项目,实现员工数据的增删改查功能,但一直卡在“CustomersGridView不存在于当前上下文”这个错误上。我已经尝试了清理视图并重建项目、修改GridView的名称、检查所有控件ID,甚至删除并重新生成了Default.aspx.designer.cs文件后再清理重建,但还是没解决问题。我谷歌了相关问题后把GridView重命名为CustomersGridView(代码如下),但错误依然存在,有没有大佬能帮我找出问题?

我的代码如下:

Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="Default.aspx.cs" Inherits="Modus_FTE_GridViewCRUD._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <asp:GridView ID="CustomersGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="FteID" AllowPaging="True" AllowSorting="True" OnRowEditing="GridView_RowEditing" OnRowUpdating="GridView_RowUpdating" OnRowCancelingEdit="GridView_RowCancelingEdit" OnRowDeleting="GridView_RowDeleting">
        <Columns>
            <asp:BoundField DataField="FteID" HeaderText="Id" ReadOnly="True" />
            <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" />
            <asp:BoundField DataField="Loc" HeaderText="Loc" />
            <asp:BoundField DataField="Hired" HeaderText="Hired" />
            <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
        </Columns>
    </asp:GridView>
    <asp:Button ID="btnAdd" runat="server" Text="Add New User" OnClick="btnAdd_Click" />
</asp:Content>

Default.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    string connStr = @"Data Source=db011;" +
                     "Initial Catalog=***DB;" +
                     "User id=***;" +
                     "Password=***;";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) BindGrid();
    }

    private void BindGrid()
    {
        SqlConnection con = new SqlConnection(connStr);
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [dbo].[Modus_FTE]", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        CustomersGridView.DataSource = dt;
        CustomersGridView.DataBind();
    }

    protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        CustomersGridView.EditIndex = e.NewEditIndex;
        BindGrid();
    }

    protected void GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        CustomersGridView.EditIndex = -1;
        BindGrid();
    }

    protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = Convert.ToInt32(CustomersGridView.DataKeys[e.RowIndex].Value);
        string name = ((TextBox)CustomersGridView.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string loc = ((TextBox)CustomersGridView.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string hired = ((TextBox)CustomersGridView.Rows[e.RowIndex].Cells[3].Controls[0]).Text;

        SqlConnection con = new SqlConnection(connStr);
        string query = "UPDATE Users SET Name=@Name, Loc=@Loc, Hired=@Hired WHERE FteID=@id";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.Parameters.AddWithValue("@Name", name);
        cmd.Parameters.AddWithValue("@Loc", loc);
        cmd.Parameters.AddWithValue("@Hired", hired);
        con.Open();
        cmd.ExecuteNonQuery();

        CustomersGridView.EditIndex = -1;
        BindGrid();
    }

    protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(CustomersGridView.DataKeys[e.RowIndex].Value);
        using (SqlConnection con = new SqlConnection(connStr))
        {
            string query = "DELETE FROM Users WHERE FteID=@id";
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.Parameters.AddWithValue("@FteID", id);
            con.Open();
            cmd.ExecuteNonQuery();
        }
        BindGrid();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(connStr))
        {
            string query = "INSERT INTO [dbo].[Modus_FTE] (FteID, Name, Loc, Hired) VALUES (@FteID, @Name, @Loc, @Hired)";
            SqlCommand cmd = new SqlCommand(query, con);
            var randOne = new Random((int)DateTime.UtcNow.Ticks); //random number based on ticks in this instance of utc time for testing purpose
            DateTime thisDay = DateTime.Today;

            cmd.Parameters.AddWithValue("@FteID", randOne);
            cmd.Parameters.AddWithValue("@Name", "New User" + randOne.ToString());
            cmd.Parameters.AddWithValue("@Loc", "new"+randOne.ToString()+"@example.com");
            cmd.Parameters.AddWithValue("@Hired", thisDay.ToString("g"));

            con.Open();
            cmd.ExecuteNonQuery();
        }
        BindGrid();
    }
}

补充说明

我的MS SQL Server目标表是[dbo].[Modus_FTE],包含FteID、Name、Loc、Hired这些字段,增删改查逻辑都是围绕这个表展开的。

我已经尝试的解决步骤:

  • 清理项目后重新生成解决方案
  • 修改GridView控件ID并同步更新后台代码中的所有引用
  • 删除Default.aspx.designer.cs文件后,通过右键页面选择“转换为Web应用程序”重新生成该文件
  • 检查页面的Inherits属性和后台类的命名空间、类名是否完全匹配

有没有大佬能帮我看看哪里还有疏漏?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 10:34:32