Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'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="server" 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?

like image 981
Skittles Avatar asked Jan 20 '23 16:01

Skittles


1 Answers

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();
  }
}
like image 166
Magnus Avatar answered Jan 29 '23 00:01

Magnus