[asp.net]错误︰ 无法找到预 TypeName 属性中指定的类型

标签: Asp.net GridView C#
发布时间: 2017/3/26 2:04:46
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个数据表与 Sql 连接和我尝试绑定,到 GridView,然而我得到错误,找不到 TypeName 属性中指定的类型。 我的 datatable 的代码看起来像这样︰

 protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{


    if (!IsPostBack)
    {

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "TestConnectionString";

        SqlCommand command = new SqlCommand();
        command.Connection = conn;
        command.CommandText = "SELECT* FROM TestTable";

        DataTable data = new DataTable();

        SqlDataAdapter adapter = new SqlDataAdapter(command);

        adapter.Fill(data);

        GridView1.DataSource = data;

    }

与.aspx 页面中的代码︰

 <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" >
        <Columns>
            <asp:BoundField DataField="Column1" HeaderText="Column1" ReadOnly="True" SortExpression="Column1" >
            <HeaderStyle Width="450px" />
            <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="Column2" HeaderText="Column2" ReadOnly="True" SortExpression="Column2" >
                <ItemStyle Width="400px" HorizontalAlign="Center" />
                </asp:BoundField>
        </Column>

    </asp:GridView>
   <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="DataTable" TypeName="data"></asp:ObjectDataSource>

我所做的事情的建议错了吗?

解决方法 1:

这并不是如何 ObjectDataSource 控制为了使用。

如果你只是想要将网格绑定在第一个页面加载,然后删除 ObjectDataSource 控制,删除 ObjectDataSource1_Selecting 事件处理程序,删除 DataSourceID 属性从 GridView ,和只需在页面加载时绑定网格︰

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid();
    }
}

private void BindGrid()
{
    using (SqlConnection conn = new SqlConnection("your connection string here"))
    using (SqlCommand command = new SqlCommand("SELECT * FROM TestTable", conn))
    {
        DataTable data = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        adapter.Fill(data);

        GridView1.DataSource = data;
        GridView1.DataBind();
    }
}

如果您想要使用 ObjectDataSource 控制,然后给它一个有效 TypeNameSelectMethod ,删除 ObjectDataSource1_Selecting 事件处理程序,并有 SelectMethod 返回的数据︰

public static class YourDataSource
{
    public static DataTable LoadSomeData()
    {
        using (SqlConnection conn = new SqlConnection("your connection string here"))
        using (SqlCommand command = new SqlCommand("SELECT * FROM TestTable", conn))
        {
            DataTable data = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            adapter.Fill(data);
            return data;
        }
    }
}

标记︰

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    TypeName="YourDataSource"
    SelectMethod="LoadSomeData" 
/>

如果您使用.NET 4.5,您也可以使用"模型绑定",从后台代码加载数据,而无需使用数据源控件︰

public DataTable LoadSomeData()
{
    using (SqlConnection conn = new SqlConnection("your connection string here"))
    using (SqlCommand command = new SqlCommand("SELECT * FROM TestTable", conn))
    {
        DataTable data = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        adapter.Fill(data);
        return data;
    }
}

标记︰

<asp:GridView ID="GridView1" runat="server" 
    AllowSorting="True" 
    AutoGenerateColumns="False"
    SelectMethod="LoadSomeData"
>
<Columns>
    ...
</Columns>
</asp:GridView>
赞助商