【问题标题】:ObjectDataSource - Null page controls in DeleteMethod / InsertMethod / UpdateMethodObjectDataSource - DeleteMethod / InsertMethod / UpdateMethod 中的空页面控件
【发布时间】:2013-08-22 17:31:26
【问题描述】:
当我尝试访问控件时,它为 NULL。
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" ... DeleteMethod="DeleteEntry">
public void DeleteEntry(long entryID)
{
try
{
... Delete ...
}
catch (Exception ex)
{
lblErrorMessage.Text = ... => lblErrorMessage is NULL!
}
}
我无法访问页面控件。
向用户返回错误消息的另一种方式是什么?
【问题讨论】:
标签:
asp.net
objectdatasource
【解决方案1】:
在您后面的表单代码中,您可以在此处捕获错误。我创建了 Exception 对象的子类并抛出它,这样我就知道我在捕捉什么。
页面代码:
protected void ObjectDataSource1_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
if (e.Exception.InnerException is MyException)
{
lblErrorMessage.Text = e.Exception.InnerException.Message;
e.ExceptionHandled = true;
}
}
}
对象代码:
try
{
// Whatever
}
catch (Exception ex)
{
throw new MyException();
}