在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.