2011-04-19 27 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

フォームデザインコードを投稿できる方が良いでしょう。 –

答えて

4

問題はGridViewDataSourceIDプロパティの値であるID MyDataSourceのフォームには、データソースコントロールがないことです。

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

上記の例では、SqlDataSourceを使用しました。ただし、すべてのデータソースコントロールが行い、そのID値はGridViewのDataSourceID属性で使用されている値と一致する必要があります。

もう1つの点は、マークアップが有効であることです。つまり、山括弧がありません。formタグの外側には何もありません。コンテンツページ外のマスターページを持つコンテンツページ上には何も表示されません。

+0

はいそれがあります – SmartestVEGA

+0

@SmartestVEGA - マークアップが有効であることを確認しましたか?フォームタグの外側に何もないことはありますか?マスターページを持つものには、コンテンツエリア外のもの(データソースコントロールを含む)は含まれていませんか? – Thomas

+0

フロントエンドソースを追加しました。見てください – SmartestVEGA

0

DataSourceIDは、ページ上のデータソースコントロールのIDである必要があります。コードビハインドでバインドする場合は、データソースIDを設定しないでください。あなたからそれを埋める場合

DataSourceID="MyDataSource" 

:ただのGridViewからこれを削除ページはマスターページを使用する場合

+0

私はコードビハインドで何もしていません – SmartestVEGA

1

にも発生することができ、ObjectDataSourceは異なるasp:content制御

+0

良い男、それは私の問題でした、ありがとう – Lambda

0

の範囲にありますcodebehind

関連する問題