'GridView1'的DataSourceID必须是IDataSource类型的控件的ID

时间:2011-04-19 16:11:23

标签: c# .net asp.net gridview ado.net

为什么我收到以下错误? 当我进行测试运行查询时,我可以看到数据!

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>

5 个答案:

答案 0 :(得分:6)

问题是表单上没有数据源控件,其ID为MyDataSource,这是DataSourceID的{​​{1}}属性的值。

GridView

在上面的示例中,我使用了<asp:GridView ID="GridView1" runat="server" DataSourceID="MyDataSource"></asp:GridView> <asp:SqlDataSource ID="MyDataSource" runat="server"></asp:SqlDataSource> 。但是,任何数据源控件都可以,并且其ID值必须与GridView的SqlDataSource属性中使用的值匹配。

要检查的另一件事是标记有效。即,DataSourceID标签之外没有丢失尖括号,没有任何内容。内容页面上没有内容页面之外的主页面。等

答案 1 :(得分:3)

当页面使用母版页并且ObjectDataSource属于不同asp:content控件的范围时,也会发生这种情况

答案 2 :(得分:0)

DataSourceID应该是页面上数据源控件的ID。如果您在代码隐藏中绑定,则不要设置DatasourceID。

答案 3 :(得分:0)

从gridview中删除它:

DataSourceID="MyDataSource"

如果你从代码隐藏

填写它

答案 4 :(得分:-2)

首先转到App_Data并创建xml文件

  • 1-App_Data / ddlxmlFile.xml

  • 2-从工具箱中获取xmldatasource并将相同的datasourceid名称赋予 xmldatasource ID =“”

将此代码应用于您的代码