How to redirect same page after some backgroud process complete using asp.net?
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(3开发者_Go百科000); 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
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 :
this.Response.Redirect(this.Request.Url.AbsolutePath);
精彩评论