The name gridview1 does not exist in the current context(名称 gridview1 在当前上下文中不存在)
问题描述
我是 C# 的新手.只需遵循 YouTube 简单示例并尝试简单地连接到 SQL 数据库.但是 GridView1
给了我一个错误.这是我的 WebForm1.aspx.cs
I am new to C#. Just following YouTube simple example and trying to simply connect to SQL database. But GridView1
gives me an error.
Thes is my 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
This is 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
And this is 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
的样子
有什么我遗漏的吗?
推荐答案
需要在Gridview中添加id="GridView1"
You need to add id="GridView1"
in Gridview
<asp:gridview id="GridView1" runat="server" OnSelectedIndexChanged="Unnamed1_SelectedIndexChanged"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
这篇关于名称 gridview1 在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:名称 gridview1 在当前上下文中不存在


- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01