开发者

Problem with datasource and gridview

开发者 https://www.devze.com 2023-03-17 15:12 出处:网络
I put this as a datasource in my gridView var source = from p in allComments select new {p.Img, p.Name, p.Comment};

I put this as a datasource in my gridView

var source = from p in allComments
         select new {p.Img, p.Name, p.Comment};
          GridView1.DataSource = source;
          GridView1.DataBind();

and i get this:

The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. 

A control with ID 'SqlDataSource1' could not be found.

My Gridview Markup:

     <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" CellPaddin开发者_StackOverflow社区g="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>


Remove DataSourceID="SqlDataSource1" from the Gridview, as you set the DataSource in Code behind..

var source = from p in allComments
select new {p.Img, p.Name, p.Comment};
GridView1.DataSource = source;
GridView1.DataBind();

You can either assign DataSourceID or DataSource, but can't do both.

Edit: Following your comments, you have a problem in Paging, to handle paging you have to to bind the data again.

 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    var source = from p in allComments
                 select new { p.Img, p.Name, p.Comment };
    GridView1.DataSource = source;
    GridView1.DataBind();
}


You've got the attribute DataSourceID="SqlDataSource1", the error message indicates that there are no SqlDataSource with the Id SqlDataSource1 in your code.

Therefore, you need to remove that attribute in order to bind it from code-behind.

Your Grid-View decleration should look like this instead:

 <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" 
    ForeColor="Black" AllowPaging="True" 
    onrowdatabound="GridView1_RowDataBound">

You can now Bind the GridView without that issue, you can only have one of them set. Either you bind from code behind using the DataSource-attribute. Or you specify the Attribute DataSourceId.


Remove DataSourceID="SqlDataSource1"

0

精彩评论

暂无评论...
验证码 换一张
取 消