简体   繁体   English

无法以声明方式设置“DataSource”属性

[英]'DataSource' property cannot be set declaratively

I'm having a bit of difficulty trying to find a more clear solution than what Google has yielded for this problem. 我试图找到一个比Google为此问题所产生的更清晰的解决方案有点困难。 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?" 我有以下代码抛出此错误,但我发现的解决方案说,“你尝试过DataSourceID而不是DataSource吗?” with no indication as to what should be used for the DataSourceID value. 没有迹象表明应该为DataSourceID值使用什么。

...leading code left out for brevity's sake
<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>
...trailing code left out for brevity's sake

So, I come to you all to ask what I should be inserting into the DataSourceID="" value. 所以,我来找你们问我应该在DataSourceID =“”值中插入什么。 Please be verbose as I am still getting my feet wet with some of this stuff. 请大家详细说明,因为我还在用这些东西弄湿我的脚。

Thanks. 谢谢。 :) :)

DataSourceID would be the id of a datasource element on your page like ObjectDataSource or SqlDataSource . DataSourceID将是您的页面上的数据源元素的ID,如ObjectDataSourceSqlDataSource

The DataSource property is used when binding to a collection of objects from codebehind. 绑定到代码隐藏中的对象集合时使用DataSource属性。

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 如果要从SQL数据库填充网格,则应将DataSourceID设置为页面上继承自DatasourceControl的控件的ID,例如SqlDatasource

To bind the DropDown in a GridView 在GridView中绑定DropDown

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();
  }
}

For quick and dirty applications you could use da DataSourceControl directly on your aspx page. 对于快速和脏的应用程序,您可以直接在aspx页面上使用da DataSourceControl。 Then you can set the DataSourceId Property of your data bound control to this control. 然后,您可以将数据绑定控件的DataSourceId属性设置为此控件。 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. 更好的方法是使用对象数据源,您可以在其中访问库中的任何.net类。

<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. 第三个选项是使用DataSource属性,该属性主要在类后面的代码的Page_Load事件中设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法以声明方式设置“ DisplayGroupTree”属性 - The 'DisplayGroupTree' property cannot be set declaratively 如何以声明方式从ComboBox的内容设置RadioButton的IsChecked属性? - How to set the IsChecked property of a RadioButton from the Contents of a ComboBox declaratively? 设置DataSource属性时,无法修改项目集合 - Items collection cannot be modified when the DataSource property is set 列表框错误:设置了DataSource属性时,无法修改项目集合 - listbox error: Items collection cannot be modified when the DataSource property is set 例外:设置DataSource属性时,无法修改项集合 - Exception : Items collection cannot be modified when the DataSource property is set 在c#中设置DataSource属性时,无法修改项目集合 - Items collection cannot be modified when the DataSource property is set in c# 设置DataSource属性时,无法修改c#项目集合 - c# Items collection cannot be modified when the DataSource property is set 无法绑定到数据源上的属性或列 - Cannot bind to the property or column on the DataSource 无法绑定到数据源上的属性或列[…] - Cannot bind to the property or column […] on the DataSource 如何设置数据源的FilterParameters属性? - How to set FilterParameters property of a datasource?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM