使用asp.net完成一些后台进程后如何重定向同一页面?
我正在创建一个应用程序,它在一个网格中一一显示学生详细信息。当用户单击 btnDetails 时,我需要启动一个线程,在几秒钟后显示下一个学生详细信息(这不是硬编码),我需要实时生成详细信息,因此需要时间,即 2-3 分钟,所以我想等待。
我已经完成了这段代码,但出现异常“响应在此上下文中不可用”,那么如何解决这个问题。
protected void btnNext_Click(object sender, ImageClickEventArgs e) { Response.Status = "301 Moved Permanently"; Response.AddHeader("Location", "http://localhost:2653/WebSite1/Default.aspx"); } public void NextStudent() { try { int iCont = 0; while (true) { Thread.Sleep(3000); iCont++; if (iCont > 5) { btnNext_Click(this, null); break; } } } catch (Exception ex) { Response.Write(ex.Message); } } protected void btnDetails_Click(object sender, EventArgs e) { Thread threadNextQuestion = new Thread(new ThreadStart(NextStudent)); threadNextQuestion.SetApartmentState(ApartmentState.STA); threadNextQuestion.Start(); }
当我手动单击 btnNext 时,它工作正常,但调用 NextStudent() 方法时出现错误。
所以请建议我如何处理这个问题。
谢谢, 拉克斯米拉尔·梅纳里亚
I am creating a application which display student details in one grid one by one. When user click on btnDetails then I need to start a thread which display next student details after few seconds (this is not hardcoded) I need to generate details realtime so it will take time i.e. 2-3 minutes, So I would like to wait.
I have done this code but getting exception "Response is not available in this context" so how to solve that.
protected void btnNext_Click(object sender, ImageClickEventArgs e) { Response.Status = "301 Moved Permanently"; Response.AddHeader("Location", "http://localhost:2653/WebSite1/Default.aspx"); } public void NextStudent() { try { int iCont = 0; while (true) { Thread.Sleep(3000); iCont++; if (iCont > 5) { btnNext_Click(this, null); break; } } } catch (Exception ex) { Response.Write(ex.Message); } } protected void btnDetails_Click(object sender, EventArgs e) { Thread threadNextQuestion = new Thread(new ThreadStart(NextStudent)); threadNextQuestion.SetApartmentState(ApartmentState.STA); threadNextQuestion.Start(); }
When I manually click on btnNext then its working fine, but calling in NextStudent() method got error.
So please suggest me how can I handle this issue.
Thanks,
Laxmilal Menaria
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只有您的主线程可以访问 Response 对象。因此,您要么需要同步调用 NextStudent,要么在线程中注册回调委托。
Only your main thread can access the Response object. So you either need to call NextStudent synchronously or register a callback delegate at your thread.
试试这个:
Try this :