抛出错误为“名称'GridView1'在当前上下文中不存在” [英] throwing an error as "The name 'GridView1' does not exist in the current context"

查看:497
本文介绍了抛出错误为“名称'GridView1'在当前上下文中不存在”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的marginalworkers.aspx为





<%@ Page Language =C#MasterPageFile =〜/ MasterPage .masterAutoEventWireup =trueCodeFile =marginalworkers.aspx.csInherits =marginalworkersTitle =Untitled Page%>



< asp :Content ID =Content1ContentPlaceHolderID =ContentPlaceHolder1Runat =Server>

边缘工人人口

< asp: GridView ID =GridView1runat =server>









和.cs文件为marginalworkers.aspx.cs

I have my marginalworkers.aspx as


<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="marginalworkers.aspx.cs" Inherits="marginalworkers" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
Population of Marginal Workers
<asp:GridView ID="GridView1" runat="server">




and a .cs file as marginalworkers.aspx.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class marginalworkers : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
    }
    public void Bind()
    {
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
        SqlDataAdapter da = new SqlDataAdapter("select * from marginalworkers", cn);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }
}



由于当前上下文中不存在名称'GridView1'而引发错误。请给我一个解决方案...


It is throwing an error as "The name 'GridView1' does not exist in the current context".Please suggest me a solution...

推荐答案

这可能与你的标签有关 - 你似乎没有关闭它们。

尝试更改此内容:

It's possible this has to do with your tags - you don't seem to close them.
Try changing this:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

对此:

To this:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server" />

并对其他控件执行相同操作,包括GridView

And do the same with your other controls, including the GridView


这个错误有三种可能的解决方案:



1 - 有两个同名的类。您可能已重命名了一个页面并创建了一个新页面,但您没有更改cs文件中的名称。因此,请确保项目中只有一个具有此名称的文件。可能有两个名称不同但类名相同的文件可能会产生问题。



2 - 项目中的多个web.config文件。确保项目中只有一个web.config文件。但是,您可以在项目中使用多个web.config文件,但必须采用特定格式。



3 - 设计器文件未刷新。(正如你所说)你正在创建一个网站,所以忽略这个)



希望它有助于:)
There are three possible solutions for this error:

1 – There are two classes with same name. Possibly you have renamed a page and created a new one, but you did not changed the name in the cs file. So make sure you have only one file with this name in your project. There may be two files with different name but same class name this may create problems.

2 – Multiple web.config files in your project. Make sure you have only one web.config file in your project. However you can have mulitple web.config files in your project but that has to be in particular format.

3 – The designer file is not refreshed.(As u said you are creating a website so ignore this)

Hope It Helps :)


这篇关于抛出错误为“名称'GridView1'在当前上下文中不存在”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆