ObjectDataSource使用初步

在使用ObjectDataSource控件的时候,可以设置其SelectMethod、InsertMethod、UpdateMethod与DeleteMethod属性,以便决定要使用业务对象的哪些方法来读取、新建、修改与删除数据,这些方法都可以接受参数。同时,GridView、DetailsView与FormView等数据绑定控件会自动创建所需参数的集合。

ObjectDataSource控件使用反射来匹配它的参数和被调用的方法的参数。参数的顺序和大小写并不重要,但是必须注意参数的名称。

1 传递参数给业务对象的InsertMethod、UpdateMethod与DeleteMethod

1.1 业务对象方法

  public DataSet GetAllList()
  {
       return GetList("");
  }

 public bool Add(string UserName, string Password)
{
            Ced.Model.User model = new Ced.Model.User();
            model.UserName = UserName;
            model.Password = Password;
            return dal.Add(model);
 }

 public bool Update(string UserName, string Password)
  {
            Ced.Model.User model = new Ced.Model.User();
            model.UserName = UserName;
            model.Password = Password;
            return dal.Update(model);
}
  public bool Delete(string UserName)
  {  
           return dal.Delete(UserName);
  }

  public Ced.Model.User GetModel(string UserName)
  {  
       return dal.GetModel(UserName);
  }

1.2 在页面中添加控件

    <asp:GridView ID="GridView1" runat="server" DataKeyNames="UserName"
        DataSourceID="ObjectDataSource1">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
                ShowSelectButton="True" />
            <asp:HyperLinkField DataNavigateUrlFields="username"
                DataNavigateUrlFormatString="Detail.aspx?username={0}" DataTextField="username"
                DataTextFormatString="{0}的详细信息" HeaderText="详细" />
        </Columns>
    </asp:GridView>

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
        DeleteMethod="Delete" InsertMethod="Add"
        SelectMethod="GetAllList" TypeName="Ced.BLL.User" UpdateMethod="Update">
    </asp:ObjectDataSource>

        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
            DataSourceID="ObjectDataSource2" Height="50px" Width="125px">
            <Fields>
                <asp:BoundField DataField="UserName" HeaderText="UserName"
                    SortExpression="UserName" />
                <asp:BoundField DataField="Password" HeaderText="Password"
                    SortExpression="Password" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
                    ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
        <asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
            DeleteMethod="Delete" InsertMethod="Add"
            SelectMethod="GetModel" TypeName="Ced.BLL.User" UpdateMethod="Update">
            <DeleteParameters>
                <asp:Parameter Name="UserName" Type="String" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="UserName" Type="String" />
                <asp:Parameter Name="Password" Type="String" />
            </UpdateParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="GridView1" Name="UserName"
                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:ObjectDataSource>

2 使用对象传递参数给业务对象的InsertMethod、UpdateMethod与DeleteMethod

2.1 给业务对象新增如下的方法,方法参数为实体对象

  public bool Add(Ced.Model.User model)
  {
       return dal.Add(model);
  }

  public bool Update(Ced.Model.User model)
  {
        return dal.Update(model);
  }

 public bool Delete(Model.User user)
 {

         return Delete(user.UserName);
}

2.2 设置ObjectDataSource的DataObjectTypeName属性

要通过实体对象来传递参数,必须将ObjectDataSource的DataObjectTypeName属性设置为自定义的实体类名称。且实体类必须有一个默认的构造方法,且公用属性必须拥有get、set访问器。同时,DataObjectTypeName属性只会影响InsertMethod、UpdateMethod与DeleteMethod,不会影响SelectMethod。

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="Ced.Model.User"
        DeleteMethod="Delete" InsertMethod="Add"
        SelectMethod="GetAllList" TypeName="Ced.BLL.User" UpdateMethod="Update">
    </asp:ObjectDataSource>

需要注意的是,设置了ObjectDataSource的DataObjectTypeName属性后,如果所绑定的业务对象没有包含参数为实体对象的InsertMethod、UpdateMethod与DeleteMethod,则会运行报错,提示找不到相应方法。

posted @ 2012-04-14 17:03  zhouhb  阅读(6046)  评论(0编辑  收藏  举报