我在GridView中問題與數據源和GridView
var source = from p in allComments
select new {p.Img, p.Name, p.Comment};
GridView1.DataSource = source;
GridView1.DataBind();
把這個作爲數據源,我得到這個:
The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource.
ID爲「SqlDataSource1」控制找不到。
我的GridView標記:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
style="z-index: 1; left: 317px; top: 374px; position: absolute; height: 597px; width: 666px"
BackColor="#CCCCCC" BorderColor="#999999"
BorderWidth="0px" CellPadding="4" CellSpacing="2"
DataSourceID="SqlDataSource1" ForeColor="Black" AllowPaging="True"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="#">
<HeaderStyle Width="500px" />
<ItemStyle Width="500px" />
<ItemTemplate>
<asp:Label ID="lblMessage" runat="server" Text='<%# Bind("Comment") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="#">
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
<ItemTemplate>
<asp:Image ID="imgName" runat="server" imageUrl='<%# Bind("Img") %>'></asp:Image><br />
<asp:Hyperlink ID="hyperLink" runat="server" Text='<%# Bind("Name") %>' ></asp:Hyperlink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我想你複製和從其他網頁粘貼代碼,並忘記刪除的DataSourceID =「SqlDataSource1」 :) –