如何从 ObjectDataSource 事件引用数据绑定控件?
以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为“OnInserted”事件应该是检查返回值或输出参数的值的事件,或者确定插入操作完成后是否引发异常的事件。返回值、输出参数和异常处理属性可从与事件关联的 ObjectDataSourceStatusEventArgs 对象中获取。
您在这里可以做的只是调用
ObjectDataSource.select()
来返回本例中的视图,但我认为这不是一个好的选择。您应该检查您的业务逻辑并尝试在更有意义的地方管理它
无论如何您的代码应该如下所示:
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:
考虑到您提供的示例,我认为没有任何东西可以替换 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.