2016-12-21 76 views
2

我是C#的新手。只需遵循YouTube簡單示例並嘗試連接到SQL數據庫。但GridView1給了我一個錯誤。 帖是我WebForm1.aspx.cs中Gridview1的名稱在當前上下文中不存在

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 有什麼我失蹤?

回答

4

您需要在GridView的

<asp:gridview id="GridView1" runat="server" OnSelectedIndexChanged="Unnamed1_SelectedIndexChanged" 
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"> 
+0

#Vinh武,我接着說:ID = 「GridView1」'在WebForm1.aspx文件。 (上面編輯)。那是對的嗎? 但我仍然在WebForm1.aspx.cs中有同樣的錯誤。對不起,它是我的第一個項目。謝謝 – Oleg

+0

有什麼錯誤? –

+0

同一個名稱GridView1在當前上下文中不存在。謝謝 – Oleg

2

添加id="GridView1"網頁設計人員有時確實拒絕控件添加到了.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 
     } 
    } 
} 
+1

非常感謝! – Oleg

相關問題