Коробка оповещения JavaScript не отображается через код за файлом C #

sharepoint.stackexchange https://sharepoint.stackexchange.com//questions/100420

Вопрос

Мне нужно реализовать функциональность, в которой сначала я должен отображать Java-скрипт от кода за файлом, а затем пользователь должен быть перенаправлен на другую страницу.

Я использовал оба

this.Context.Response.Write("<script type='text/javascript'>alert('some text')</script>");
.

и

ClientScriptManager.RegisterStartupScript();
.

Но ни один из них не показал оповещение фрагмент кода как:

//approach 1
    this.Context.Response.Write("<script type='text/javascript'>alert('some text')</script>");
  SPUtility.Redirect(string.Empty, SPRedirectFlags.UseSource, this.Context);

//approach 2
String csname1 = "PopupScript";
                Type cstype = this.GetType();

                // Get a ClientScriptManager reference from the Page class.
                ClientScriptManager cs = Page.ClientScript;

                // Check to see if the startup script is already registered.
                if (!cs.IsStartupScriptRegistered(cstype, csname1))
                {
                    StringBuilder cstext1 = new StringBuilder();
                    cstext1.Append("<script type=text/javascript> alert('You do not have Access to perform any action.') </");
                    cstext1.Append("script>");

                    cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
                }
  SPUtility.Redirect(string.Empty, SPRedirectFlags.UseSource, this.Context);
.

Но ни один из них не показывает предупреждающую коробку

Пожалуйста, помогите мне на улицу

Это было полезно?

Решение

The script will never make it to the client because of the redirect. You either need to send the script with the alert and then postback to perform the redirect (yuck) or perform the redirect in the script by doing a location.replace to the URL you want to redirect to...you could also set location.href to the URL as well.

So you could do something like this:

ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptname", "alert('some message'");location.replace('url to redirect to');", true);

Другие советы

These methods registers and adds the script block to the page. So it will fire after the page is rendered from the server. In your case you are redirecting to some other page while the script is registered on the previous page.

So it would not fire on the previous page.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top