2011-04-19 374 views
1

爲什麼我會收到跟隨錯誤? 我可以看到數據,當我做測試運行查詢!'GridView1'的DataSourceID必須是IDataSource類型的控件的ID

Server Error in '/' Application. 
The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'MyDataSource' could not be found. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'MyDataSource' could not be found. 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 

[HttpException (0x80004005): The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'MyDataSource' could not be found.] 
    System.Web.UI.WebControls.DataBoundControl.GetDataSource() +8556294 
    System.Web.UI.WebControls.DataBoundControl.ConnectToDataSourceView() +37 
    System.Web.UI.WebControls.DataBoundControl.OnLoad(EventArgs e) +19 
    System.Web.UI.Control.LoadRecursive() +50 
    System.Web.UI.Control.LoadRecursive() +141 
    System.Web.UI.Control.LoadRecursive() +141 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627 


Version Information: Microsoft .NET Framework Version:2.0.50727.4927; ASP.NET Version:2.0.50727.4927 

下面是我的源:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
    </div> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     DataSourceID="MyDataSource"> 
     <Columns> 
      <asp:BoundField DataField="Colum1" HeaderText="Colum1" 
       SortExpression="Colum1" /> 
      <asp:BoundField DataField="Column2" HeaderText="Column2" 
       SortExpression="Column2" /> 
      <asp:BoundField DataField="Column3" HeaderText="Column3" 
       SortExpression="Column3" /> 
     </Columns> 
    </asp:GridView> 
    </form> 
</body> 
</html> 
+0

如果您可以發佈表單設計代碼,會更好。 – 2011-04-19 16:18:54

回答

4

的問題是,不存在與ID MyDataSource這是GridViewDataSourceID屬性的值的形式上沒有數據源的控制。

<asp:GridView ID="GridView1" runat="server" DataSourceID="MyDataSource"></asp:GridView> 
<asp:SqlDataSource ID="MyDataSource" runat="server"></asp:SqlDataSource> 

在上面的例子中,我使用了一個SqlDataSource。但是,任何數據源控件都會執行並且其ID值必須與GridView的DataSourceID屬性中使用的值匹配。

另一件要檢查的是標記是有效的。也就是說,沒有缺失的尖括號,form標籤外沒有任何東西。母版頁位於內容區域之外的內容頁面上沒有任何內容。等等。

+0

是的,它是有 – SmartestVEGA 2011-04-19 16:17:21

+0

@SmartestVEGA - 你是否證實標記是有效的?表單標籤之外什麼也沒有?主頁面的任何內容都不包含內容區域以外的內容(包括數據源控件)? – Thomas 2011-04-19 16:20:58

+0

我已添加前端源..請看看 – SmartestVEGA 2011-04-19 16:30:41

0

DataSourceID應該是頁面上數據源控件的ID。如果您在代碼隱藏中綁定,則不要設置DatasourceID。當網頁使用了母版和ObjectDataSource是在不同asp:content控制的範圍

+0

我沒有做任何代碼隱藏 – SmartestVEGA 2011-04-19 16:17:43

1

也會發生這種情況

+0

好人,那是我的問題,謝謝 – Lambda 2015-02-11 17:08:05

0

只是在GridView刪除此:

DataSourceID="MyDataSource" 

如果從填充代碼隱藏

相關問題