【问题标题】:asp.net ObjectDataSource Update from code behind从后面的代码更新 asp.net ObjectDataSource
【发布时间】:2013-10-11 06:30:19
【问题描述】:

我有一个 ASP.NET 应用程序,在一个页面上有一个 gridview,它使用 ObjectDataSource 调用位于另一个名为“Device”的类中的方法。那么这个类返回一个特殊的数据表。这是有效的。

现在我需要一个更新数据的方法,为此我不能使用 Device 类,所以我想从后面的代码中使用该网格的“RowUpdating”方法。我什至为此方法编写了代码,如果用户单击网格中的“更新”按钮,它正在工作并且该方法会触发。

现在解决问题。因为我正在使用 ObjectDataSource 并且我在该网格中有一个更新命令,所以我还需要为此 ObjectDataSource 指定和更新方法,这就是重点。 我想使用后面代码中的 RowUpdating 方法,而不是该 ObjectDataSource 中的更新方法。我当前的解决方案是“设备”类中的一个方法,它只是通过“返回”命令返回,所以我后面的代码中的 RowUpdating 正在完成这项工作......但这不能是“它的唯一工作方式”。

帮助和进一步的提示将不胜感激。 谢谢!

【问题讨论】:

    标签: c# asp.net objectdatasource


    【解决方案1】:

    如果您使用 ObjectDataSource,则必须在一个类中编写更新和选择方法。也许您必须直接为该 GridView 编写一个类来将更新和选择方法委托给其他对象。

    我认为这对你有用:

    [DataObject(true)]
    public class SomeService
    {
        private Device d;
        private YourUpdaterClass yuc;
    
        public SomeService()
        {
            this.d = new Device();
            this.yuc = new YourUpdaterClass();
        }
    
        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public List<YourType> Select()
        {
            return d.YourSelectMethod();
        }
    
        [DataObjectMethod(DataObjectMethodType.Update, true)]
        public void Update(YourType yt)
        {
            yuc.YourUpdateMethod(yt);
        }
    }
    

    还有 ObjectDataSource:

    <asp:ObjectDataSource 
       ID="ObjectDataSource1"
       runat="server" 
       SelectMethod="Select"
       TypeName="SomeService"
       DataObjectTypeName="YourType"
       UpdateMethod="Update">
    </asp:ObjectDataSource>
    

    【讨论】:

    • 嗯,谢谢你的回答,但是不能使用网格的更新事件吧?使用您提出的解决方案,我可以解决我的问题,但这需要对我的架构进行一些更改
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    相关资源
    最近更新 更多