如何从 ObjectDataSource 事件引用数据绑定控件?

发布于 2024-10-08 15:04:39 字数 798 浏览 4 评论 0原文

以使用 ObjectDataSource 作为数据源的 DetailsView 控件为例。

通常在 DetailsView.ItemUpdated 事件中,我会通过转换 sender 来获取对详细信息视图的引用:

DetailsView dv = (DetailsView)sender;

在某些情况下,有必要在 中处理事件ObjectDataSource.ItemUpdated 事件。在本例中,sender 现在的类型为 ObjectDataSource。我想要做的是编写干净的代码,而不是像

Label label1 = DetailsView1.FindControl("Label1");

我查看文档那样硬编码,并且还进行了一些搜索,但找不到如何编写如下代码:

protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
   ObjectDataSource ods = (ObjectDataSource)sender;
   DetailsView dv = (DetailsView)ods.SOMETHING_HERE;
}

有谁知道我应该在 < 中放入什么上面代码片段中的 code>SOMETHING_HERE ?

Take for example a DetailsView control with an ObjectDataSource as its datasource.

Normally in the DetailsView.ItemUpdated event I would grab a reference to the details view by casting the sender:

DetailsView dv = (DetailsView)sender;

In certain situations it becomes necessary to handle the event inside the ObjectDataSource.ItemUpdated event. In this case sender is now of type ObjectDataSource. What I want to be able to do is write clean code that isnt hardcoded like

Label label1 = DetailsView1.FindControl("Label1");

I looked over the documentation and also did some searches but couldnt find how I would write some code like the following:

protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
   ObjectDataSource ods = (ObjectDataSource)sender;
   DetailsView dv = (DetailsView)ods.SOMETHING_HERE;
}

Does anyone know what I should be putting in the SOMETHING_HERE in the snippet above?

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

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

发布评论

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

评论(2

指尖凝香 2024-10-15 15:04:39

发生这种情况是因为“OnInserted”事件应该是检查返回值或输出参数的值的事件,或者确定插入操作完成后是否引发异常的事件。返回值、输出参数和异常处理属性可从与事件关联的 ObjectDataSourceStatusEventArgs 对象中获取。

您在这里可以做的只是调用 ObjectDataSource.select() 来返回本例中的视图,但我认为这不是一个好的选择。
您应该检查您的业务逻辑并尝试在更有意义的地方管理它

无论如何您的代码应该如下所示:

ObjectDataSource ods = YourDataSource.select(); 
DetailsView dv = (DetailsView)ods; 

That's happen because the "OnInserted" event is suppose to be an event examine the values of a return value or output parameters, or to determine whether an exception was thrown after an Insert operation has completed. The return value, output parameters, and exception handling properties are available from the ObjectDataSourceStatusEventArgs object that is associated with the event.

What you can do here is just call ObjectDataSource.select() that returns the view in this case but I don't think it's a good choice.
You should review you business logic and try to manage it somewhere it makes more sense

Anyway your code should look like the below:

ObjectDataSource ods = YourDataSource.select(); 
DetailsView dv = (DetailsView)ods; 
带上头具痛哭 2024-10-15 15:04:39

考虑到您提供的示例,我认为没有任何东西可以替换 Something_Here。这是与 DV 相关联的 ODS,而不是相反。此外,一个数据源可以链接到多个数据绑定控件。
据我所知,这是不可能的。

Considering the example you provided, I don't think there is anything you can replace for Something_Here. It is the ODS linked to DV and not the other way. Also one DataSource can be linked to several DataBound Controls.
So as far as I know it is simply not possible.

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