如何向BeginAsyncOperation返回的页面显示错误消息?页面正常加载没有数据,但用户不知道为什么?我验证了错误已被抛出但未在BeginAsyncOperation中处理。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AddOnPreRenderCompleteAsync(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation)
);
}
}
private IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback callback, object state)
{
var t = new ThreadStart(GetData);
try
{
return t.BeginInvoke(callback, null);
}
catch (Exception ex)
{
this.lblError.Text = ex.Message;
this.pnlError.Visible = true;
}
return null;
}
private void EndAsyncOperation(IAsyncResult result)
{
BindData();
SetControlStates();
}
private void GetData()
{
try
{
throw new Exception("something broke!");
}
catch (Exception ex)
{
throw ex;
}
}
答案 0 :(得分:0)
我过度思考了。
private IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback callback, object state)
{
var t = new ThreadStart(GetData);
return t.BeginInvoke(callback, null);
}
private void GetData()
{
try
{
throw new Exception("something broke!");
}
catch (Exception ex)
{
this.lblError.Text = ex.Message;
this.pnlError.Visible = true;
}
}