开发者

'DataSource' property cannot be set declaratively

I have the following code that is throwing this error but the solutions I've found say, "Have you tried the DataSourceID instead of DataSource?" with no indication as to what should be used for the DataSourceID value.

<Columns>
    <asp:BoundColumn DataField="id" SortExpression="id" HeaderText="ID" ItemStyle-CssClass="dgCells"></asp:BoundColumn>
      <asp:BoundColumn DataField="first_name" SortExpression="first_name" HeaderText="First" ItemStyle-CssClass="dgCells"></asp:BoundColumn>
      <asp:BoundColumn DataField="last_name" SortExpression="last_name" HeaderText="Last" ItemStyle-CssClass="dgCells"></asp:BoundColumn>
      <asp:BoundColumn DataField="login_pw" HeaderText="Password" ItemStyle-CssClass="dgCells"></asp:BoundColumn>
      <asp:TemplateColumn HeaderText="Race">
        <ItemTemplate>
          <%# DataBinder.Eval(Container.DataItem, "race_name") %>
        </ItemTemplate>
        <EditItemTemplate>
          <asp:DropDownList runat="s开发者_如何学Pythonerver" id="ddlRaces" DataValueField="race_id" DataTextField="race_name" >>>DataSourceID=""<<< />
        </EditItemTemplate>
      </asp:TemplateColumn>
    <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Ok"></asp:EditCommandColumn>
</Columns>

What I should be inserting into the DataSourceID="" value?


The DataSourceID should be set to the ID of a control on your page that inherits from DatasourceControl such as SqlDatasource if you want to populate the grid from an SQL database

To bind the DropDown in a GridView

protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType == DataControlRowType.DataRow)
  {  
    var ddl = (DropDownList)item.FindControl("ddlRaces");
    ddl.Datasource = GetRaces();
    ddl.DataBind();
  }
}


DataSourceID would be the id of a datasource element on your page like ObjectDataSource or SqlDataSource.

The DataSource property is used when binding to a collection of objects from codebehind.


For quick and dirty applications you could use da DataSourceControl directly on your aspx page. Then you can set the DataSourceId Property of your data bound control to this control. For larger applications it is not advisable to use this technique, because you have no separation between your user interface and your business or data access code.

<asp:SqlDataSource
    id="SqlDataSource1"
    runat="server"
    DataSourceMode="DataReader"
    ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
    SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
</asp:SqlDataSource>

<asp:GridView
    id="GridView1"
    runat="server"
    DataSourceID="SqlDataSource1">
</asp:GridView>

A better way would be to use an object data source where you can access any .net class in your libraries.

<asp:ObjectDatasource
    id="ObjectDataSource1"
    runat="server"
    selectmethod="GetAllEmployees"
    typename="Samples.AspNet.EmployeeLogic" />

And the third option is to use the DataSource property which is mainly set in the Page_Load event of the code behind class.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜