当前上下文中不存在名称 gridview1

2023-12-09

我是 C# 新手。只需遵循 YouTube 简单示例并尝试简单地连接到 SQL 数据库。但GridView1给我一个错误。 这是我的 WebForm1.aspx.cs

using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data.Odbc;



namespace adoDemo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String CS = "data source =.; database = AdventureWorks2016CTP3; integrated security = SSPI";
            SqlConnection con = new SqlConnection(CS);
            SqlCommand cmd = new SqlCommand("select top 5 * from [Sales].[CreditCard]", con);
            con.Open();
            GridView1.DataSource = cmd.ExecuteReader();
            GridView1.DataBind();
            con.Close();
        }
    }
}

这是WebForm.aspx.designer.cs

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------

namespace adoDemo {


    public partial class WebForm1 
    {

        /// <summary>
        /// form1 control.
        /// </summary>
        /// <remarks>
        /// Auto-generated field.
        /// To modify move field declaration from designer file to code-behind file.
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlForm form1;
    }
}

这是WebForm1.aspx

   <script runat="server">

    Protected Sub Unnamed1_SelectedIndexChanged(sender As Object, e As EventArgs)

    End Sub
</script>
body 

<asp:gridview id="GridView1" runat="server" OnSelectedIndexChanged="Unnamed1_SelectedIndexChanged" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FFF1D4" />
    <SortedAscendingHeaderStyle BackColor="#B95C30" />
    <SortedDescendingCellStyle BackColor="#F1E5CE" />
    <SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:gridview>

这就是我的WebForm1.aspx.designer.cs好像

enter image description here Is there anything I'm missing?


页面设计者有时确实会拒绝向 .designer.cs 文件添加控件;我已经见过很多次了。

如果发生这种情况,请自行将类级变量添加到 .aspx.cs 中:-

namespace adoDemo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        // make sure its's the same spelling & case as in the .aspx
        protected GridView GridView1;

        protected void Page_Load(object sender, EventArgs e)
        {
            // etc
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当前上下文中不存在名称 gridview1 的相关文章

随机推荐

  • Angular ui-router 按刷新会导致 404 错误

    好的 我知道这是一个开放式问题 但是 我正在使用 AngularJS 1 4 x 和 ui router 运行一个应用程序 在大多数情况下 一切都正常并符合预期 我的各个页面使用 ui sref 进行导航 页面按预期显示 并且显示的 URL
  • 如何在表单 POST 或 GET 后保留选择框的选定值

    我正在尝试在我的网站中实现搜索功能 当在文本框中输入搜索关键字并选择类别组合时 表单将被发布 结果将显示在同一页面上 我想要的是在发布后默认在表单中保留组合的选定类别 例如 如果我在组合中选择 汽车 类别并单击搜索 则在表单提交后 组合应将
  • 将多维数组转换为对象

    我有一个最多包含 32 32 个条目的二维数组 我想将它从这样的东西转换 null null null null null null null null null null null null null null null null pla
  • Groovy 正则匹配引号之间的所有内容

    我有这个正则表达式 regex 所以我正在寻找引号之间的所有文本 现在我有以下字符串 options a 2 s 10 Print Type s 8 New Book s 8 Template s 9 See Notes 不管怎样做 reg
  • 将产品自定义字段保存为 WooCommerce 管理员手动订单的自定义订单项目元数据

    Using 将自定义产品元数据传递到 Woocommerce 3 中的订单回答代码 从后端手动创建订单时 从后端手动添加产品时是否可以保存和显示自定义元数据 这是我的代码 稍作改动 Admin products Display custom
  • 在 Acumatica 中,您可以使用未绑定的 DAC 获得图表/页面吗?

    是否可以让图表和页面使用完全未绑定的 DAC 当前在 4 20 中尝试此操作时 我收到以下错误消息 Incorrect syntax near the keyword FROM Description An unhandled except
  • ListView 未取消选择

    我正在使用 ListView 开发 Xamarin Forms 项目 ListView 的 XAML 是
  • R:用先前的值填充缺失值[重复]

    这个问题在这里已经有答案了 我有一个如下所示的数据框 d lt data frame county c Abilene rep NA 5 Cook rep NA 4 Blah NA Allegheny rep NA 3 county 1 A
  • 如何修复 Visual Studio 中的“找不到类型或命名空间名称”错误?

    我得到一个 找不到类型或命名空间名称 VS2010 中的 C WPF 应用程序出现错误 这部分代码编译得很好 但突然我收到了这个错误 我尝试删除项目参考和using声明 关闭VS2010并重新启动 但我仍然有这个问题 任何想法为什么会发生这
  • 使用 AVAudioRecorder 录制 AVAudioPlayer 输出

    在我的应用程序中 用户通过按下按钮来播放声音 有多个按钮可以同时播放 声音是使用 AVAudioPlayer 实例播放的 我想使用 AVAudioRecorder 记录这些实例的输出 我已经完成所有设置并创建了一个文件并进行录制 但是当我播
  • Matplotlib:子图中的插图

    我必须做几个子图 每个子图同时有一个插图custom相对于子图的位置 原则上可以通过axes grid1如中给出的this然而 例如 位置参数loc in inset axes or zoomed inset axes对于我的目的来说不够具
  • AlertDialog 上的 AutoCompleteTextView 实现

    我在 OnClickListener 方法中实现了 AutoCompleteTextView 但是 一旦单击 AutoCompleteTextView 我就看不到下拉文本 并且键盘仍然出现 我喜欢的是出现下拉文本 而键盘应该是不可见的 可能
  • lucene 短语查询不起作用

    我正在尝试使用 Lucene 2 9 4 编写一个简单的程序 它搜索短语查询 但我得到 0 次点击 public class HelloLucene public static void main String args throws IO
  • Powershell正则表达式用于两个特殊字符之间的字符串

    文件名如下 inpFiledev abc XYZ bak 我只需要变量中的 XYZ 即可与其他文件名进行比较 我试过下面 String findev regex match inpFiledev Value Write Host finde
  • Rails gem 将一个段落分成一系列句子

    我试图将一个段落分成一系列句子 使每个句子组保持在 N 个字符以下 如果单个句子的长度超过 N 则应将其分成多个块 并以标点符号或空格作为分隔符 例如 如果 N 50 则以下字符串 Lorem ipsum consectetur elit
  • 从相机胶卷图像创建视频 - iOS sdk

    我使用以下代码从图像创建视频 当我从从网络下载的相机胶卷或屏幕截图中选择图像时 此代码工作正常 但从相机拍摄的所选图像显示在电影中放大 我不知道相机的图像有什么问题 谁能帮我解决这个问题 IBAction createV id sender
  • 如何修复以下 Django 错误:“类型:IOError”“值:[Errno 13] 权限被拒绝”

    我正在遵循 Django 教程 在该教程中 一旦图像保存在管理中 您就需要构建一些图像缩略图 我还使用 Python 的 tempfile 模块来保存临时文件名 但是我不断遇到以下错误 Type IOError Value Errno 13
  • 为什么三元运算符不支持块?

    为什么三元运算符没有块 换句话说 为什么下面的代码不起作用并报告错误 braces int main int i 1 i 1 printf Hello n printf World n return 0 EDIT 也许这个问题被误解了 问题
  • Android 矢量绘图应用程序:srcCompat 不显示图像

    我正在使用支持库在 android kitkat 上显示矢量图像 当我在模拟器上测试我的应用程序时 我没有看到任何这些图像 我为 android lollipop 及以上做了一个单独的布局 它工作得很好 我想因为我正在使用src属性而不是s
  • 当前上下文中不存在名称 gridview1

    我是 C 新手 只需遵循 YouTube 简单示例并尝试简单地连接到 SQL 数据库 但GridView1给我一个错误 这是我的 WebForm1 aspx cs using System Linq using System Web usi