在aspx页面我有:如何在代码隐藏中创建SqlDataSource时将SqlDataSource引用到GridView控件?
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
</asp:GridView>
在代码隐藏页:
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager
.AppSettings["MyConnectionString"];
SqlDataSource1.SelectCommand = "SELECT * FROM my_table_name";
this.Controls.Add(SqlDataSource1);
}
会抛出服务器错误:
The DataSourceID of '
GridView1
' must be the ID of a control of type IDataSource. A control with ID 'SqlDataSource1
' could not be found.