Предупреждение Дисплея Веб-формы Asp.net и перенаправление

Не столько решение, сколько обходной путь (предложенный derhass в комментариях) - разделить рендер на несколько меньших рендеров. Я рисую изображение в горизонтальных полосах высотой 10 пикселей и вызываю wxSafeYield () между ними, чтобы пользовательский интерфейс оставался отзывчивым (и пользователь может прервать рендеринг, если он занимает слишком много времени).

Дополнительным преимуществом является меньшая опасность перегрузки графического процессора. Перед выполнением этого я однажды запустил длинный рендеринг, который заставил мои дисплеи отключиться, и мне пришлось сделать полную перезагрузку.

7
задан AlteredConcept 23 April 2009 в 15:47
поделиться

3 ответа

You can't do a Response.Redirect because your javascript alert will never get displayed. Better to have your javascript code actually do a windows.location.href='default.aspx' to do the redirection after the alert is displayed. Something like this:

protected virtual void DisplayAlert(string message)
{
    ClientScript.RegisterStartupScript(
      this.GetType(),
      Guid.NewGuid().ToString(),
      string.Format("alert('{0}');window.location.href = 'default.aspx'", 
        message.Replace("'", @"\'").Replace("\n", "\\n").Replace("\r", "\\r")),
        true);
}
7
ответ дан 6 December 2019 в 12:54
поделиться

The DisplayAlert method adds the client script to the currently executing page request. When you call Response.Redirect, ASP.NET issues a HTTP 301 redirect to the browser, therefore starting a new page request where the registered client script no longer exists.

Since your code is executing on the server-side, there is no way to display the alert client-side and perform the redirect.

Also, displaying a JavaScript alert box can be confusing to a user's mental workflow, an inline message would be much more preferable. Perhaps you could add the message to the Session and display this on the Default.aspx page request.

protected void Save(..)
{   
    // Do save stuff
    Session["StatusMessage"] = "The changes were saved Successfully";
    Response.Redirect("Default.aspx");
}

Then in Default.aspx.cs code behind (or a common base page class so this can happen on any page, or even the master page):

protected void Page_Load(object sender, EventArgs e)
{
    if(!string.IsNullOrEmpty((string)Session["StatusMessage"]))
    {
        string message = (string)Session["StatusMessage"];
        // Clear the session variable
        Session["StatusMessage"] = null;
        // Enable some control to display the message (control is likely on the master page)
        Label messageLabel = (Label)FindControl("MessageLabel");
        messageLabel.Visible = true;
        messageLabel.Text = message;
    }
}

The code isn't tested but should point you in the right direction

4
ответ дан 6 December 2019 в 12:54
поделиться
protected void Save(..)
{       
    // Do save stuff    
    ShowMessageBox();  
} 

private void ShowMessageBox()
{        
    string sJavaScript = "<script language=javascript>\n";        
    sJavaScript += "var agree;\n";        
    sJavaScript += "agree = confirm('Do you want to continue?.');\n";        
    sJavaScript += "if(agree)\n";        
    sJavaScript += "window.location = \"http://google.com\";\n";        
    sJavaScript += "</script>";      
    Response.Write(sJavaScript);
}  
1
ответ дан 6 December 2019 в 12:54
поделиться
Другие вопросы по тегам:

Похожие вопросы: