2
votes

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

2

2 Answers

1
votes

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.

1
votes

Try this :

this.Response.Redirect(this.Request.Url.AbsolutePath);