具有相同处理程序的详细信息查看项目事件(更新删除)

发布于 2024-09-09 18:48:15 字数 1084 浏览 2 评论 0原文

我在我的 ASP.NET Web 应用程序中使用详细信息视图。

当它触发更新和插入的事件时,我想以同样的方式处理它们。因为它们拥有的事件参数是两个独立的类,没有共同的继承,所以我必须有两个具有基本相同代码的独立方法。

有什么办法解决这个问题吗?

例如。

protected void DetailsViewItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        if (e == null)
            return;

        if (e.Exception != null)
        {
            e.ExceptionHandled = HandleException(e.Exception);
            e.KeepInInsertMode = e.ExceptionHandled;
        }
    }

    protected void DetailsViewItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e == null)
            return;

        if (e.Exception != null)
        {
            e.ExceptionHandled = HandleException(e.Exception);
            e.KeepInEditMode = e.ExceptionHandled;
        }
    }

我想提取 如果(e == null) 返回;

        if (e.Exception != null)
        {
            e.ExceptionHandled = HandleException(e.Exception);
            e.KeepInEditMode = e.ExceptionHandled;
        }

如果可能的话,采用某种通用方法。

I'm using a details view in my asp.net web application.

When it fires its updated and inserted events I'd like to handle them the same way. Because the event args they have are two separate classes with no common inheritance I have to have two separate methods with basically the same code.

Is there any way around this?

eg.

protected void DetailsViewItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        if (e == null)
            return;

        if (e.Exception != null)
        {
            e.ExceptionHandled = HandleException(e.Exception);
            e.KeepInInsertMode = e.ExceptionHandled;
        }
    }

    protected void DetailsViewItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e == null)
            return;

        if (e.Exception != null)
        {
            e.ExceptionHandled = HandleException(e.Exception);
            e.KeepInEditMode = e.ExceptionHandled;
        }
    }

I'd like to extract
if (e == null)
return;

        if (e.Exception != null)
        {
            e.ExceptionHandled = HandleException(e.Exception);
            e.KeepInEditMode = e.ExceptionHandled;
        }

into some kind of common method if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别念他 2024-09-16 18:48:15

我同意这些类直接继承自 EventArgs 是令人失望的。看起来合乎逻辑的是,它们将从 EventArgs 的同一子类继承,不仅考虑到它们的共同属性,而且事实上,大多数时候您使用 DetailsView 进行插入,也使用它进行更新。

话虽如此,您想要做的事情可以通过一些动态 C# 来完成。它非常酷,甚至可能有点优雅,尽管开销更大。

试试这个:(需要 C# 4.0)

protected void DetailsView1_Inserted(Object sender, DetailsViewInsertedEventArgs e)
{
    ProcessDetailsViewEventArgs(e);
}

protected void DetailsView1_Updated(Object sender, DetailsViewUpdatedEventArgs e)
{
    ProcessDetailsViewEventArgs(e);
}

private void ProcessDetailsViewEventArgs(dynamic e)
{
    if (e == null)
        return;

    if (e.Exception != null)
    {
        e.ExceptionHandled = HandleException(e.Exception);
        e.KeepInEditMode = e.ExceptionHandled;
    }

}

I agree it is disappointing that these classes inherit directly from EventArgs. It seems logical that they would inherit from the same sublass of EventArgs given not just their common properties, but the fact that most times you use a DetailsView for inserting you also use it for updating.

That being said, what you want to do can be accomplished with a bit of dynamic C#. It's pretty cool, and maybe even a little elegant, albeit a little more overhead.

Try this: (requires C# 4.0)

protected void DetailsView1_Inserted(Object sender, DetailsViewInsertedEventArgs e)
{
    ProcessDetailsViewEventArgs(e);
}

protected void DetailsView1_Updated(Object sender, DetailsViewUpdatedEventArgs e)
{
    ProcessDetailsViewEventArgs(e);
}

private void ProcessDetailsViewEventArgs(dynamic e)
{
    if (e == null)
        return;

    if (e.Exception != null)
    {
        e.ExceptionHandled = HandleException(e.Exception);
        e.KeepInEditMode = e.ExceptionHandled;
    }

}
追我者格杀勿论 2024-09-16 18:48:15

使用 OnItemCommand, &给你的编辑和删除按钮 CommandNames。此事件将为您处理这两种情况。

Use the OnItemCommand, & give your Edit & Delete buttons CommandNames. This event will handle both scenarios for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文