I need help on a section of the code below to revise so instead of redirect to a full page I want it to redirect and open as a popup window. How can i change the code I have to make this possible.

void context_AcessDenied(object sender, EventArgs e)
        {
            try
            {
                HttpApplication httpApp = sender as HttpApplication;
                HttpContext context = httpApp.Context;

                string httpUrl = context.Request.Url.ToString();   
                string strSiteURL = string.Empty;
                string strWebURL = string.Empty;
                string strOwnerName = string.Empty;
                string strOwnerEmail = string.Empty;

                if (httpUrl.ToLower().Contains("/_layouts/accessdenied.aspx") && !httpUrl.ToLower().Contains("loginasanotheruser=true"))
                {
                    strSiteURL = SPContext.Current.Web.Url;

                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(strSiteURL))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                strWebURL = web.Url;
                                SPGroupCollection groups = web.Groups;

                                foreach (SPGroup group in groups)
                                {
                                    if (group.Name.ToLower().Contains("owner") && !group.Name.ToLower().Contains("sub-owner") && group.Users.Count > 0)
                                    {
                                        SPUser owner = group.Users[0];
                                        strOwnerName = owner.Name;
                                        strOwnerEmail = owner.Email;
                                    }
                                }
                            }
                        }
                    });

                    HttpContext.Current.Server.ClearError();
                    HttpContext.Current.Response.Clear();
                    //strCustomAcssDndURL = getAppSettingsValue(m_SiteCollCustAcsDndPG + "-" + strSiteURL);
                    //HttpContext.Current.Response.Redirect(strCustomAcssDndURL, false);

                    string redirect = getAppSettingsValue(m_SiteCollCustAcsDndPG);
                    redirect += "?ReqURL=" + strWebURL;

                    if (!string.IsNullOrEmpty(strOwnerEmail))
                        redirect += "&OwnEmail=" + strOwnerEmail;

                    if(!string.IsNullOrEmpty(strOwnerName))
                        redirect += "&OwnName=" + strOwnerName;

                    HttpContext.Current.Response.Redirect(redirect, false);                    
                }
            }
            catch (Exception ex)
            {
                //Do your exception handling

                HttpContext.Current.Server.ClearError();
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Redirect("/Pages/AccessDeniedOverride.aspx?Error=true&Message=" + ex.Message);
            }
        }

Dani AI

Generated

You cannot make Response.Redirect open a popup. Redirect sends a 302 to the browser; only client-side script can open a new window. Also, dropping a <script> tag into the URL you pass to Redirect will never work. is right that you need window.open, but in your case (this runs in HttpApplication, not a Page) you should write a small HTML response that runs the script, then stop the pipeline. ’s ScriptManager approach will not fire here because there is no Page instance.

Try replacing your redirect block with something like this (note the URL encoding and CompleteRequest):

var baseUrl = getAppSettingsValue(m_SiteCollCustAcsDndPG);
var qs = "ReqURL=" + HttpUtility.UrlEncode(strWebURL);
if (!string.IsNullOrEmpty(strOwnerEmail)) qs += "&OwnEmail=" + HttpUtility.UrlEncode(strOwnerEmail);
if (!string.IsNullOrEmpty(strOwnerName)) qs += "&OwnName=" + HttpUtility.UrlEncode(strOwnerName);
var popupUrl = baseUrl + (baseUrl.Contains("?") ? "&" : "?") + qs;

var resp = HttpContext.Current.Response;
resp.Clear();
resp.ContentType = "text/html";
resp.Write("<!doctype html><html><body>");
resp.Write("<script>var u='" + popupUrl + "'; window.open(u,'accessDenied','noopener');</script>");
resp.Write("<noscript><a href='" + HttpUtility.HtmlAttributeEncode(popupUrl) + "'>Continue</a></noscript>");
resp.Write("</body></html>");
HttpContext.Current.ApplicationInstance.CompleteRequest();

Notes:

  • Do not call Response.Redirect after writing the script; pick one approach.
  • Popups opened on page load may be blocked by modern browsers. If you want a fallback, add window.location.replace(u); after window.open(...) to navigate in the current tab when the popup is blocked.
  • If you truly need a modal, consider an in-page dialog instead of a separate window; popups are unreliable unless triggered by a user click.

Recommended Answers

All 6 Replies

Try the following code

string url="http://www.google.com";
Response.Write("<script>window.open('" +url+ "');</script>");

I have just find out the following code check this too. You need to add the following to your server side link/button:

OnClientClick="aspnetForm.target ='_blank';"

The entire button code will look something like:

<asp:LinkButton ID="myButton" runat="server" Text="Click Me!" OnClick="myButton_Click" OnClientClick="aspnetForm.target ='_blank';"/>

Hi,
You can try this as well,

In the code behind where you perform the redirect operation, instead try something like this,

Dim jsStr As String = "window.open('" +url+ "');"
ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType(), "", jsStr, True)

Try the following code

string url="http://www.google.com";
Response.Write("<script>window.open('" +url+ "');</script>");

Just trying to see how I could apply this code to my code:

I updated this:

redirect += "?ReqURL=" + strWebURL;

to this this:

string redirect = getAppSettingsValue(m_SiteCollCustAcsDndPG);
                    [B]redirect += ("<script>window.open?ReqURL=" + strWebURL + ");</script>");[/B]

                    if (!string.IsNullOrEmpty(strOwnerEmail))
                        redirect += "&OwnEmail=" + strOwnerEmail;

                    if(!string.IsNullOrEmpty(strOwnerName))
                        redirect += "&OwnName=" + strOwnerName;

                    HttpContext.Current.Response.Redirect(redirect, false);                    
                }
            }
            catch (Exception ex)
            {
                //Do your exception handling

                HttpContext.Current.Server.ClearError();
                HttpContext.Current.Response.Clear();

                string url = "/Pages/AccessDeniedOverride.aspx";
             HttpContext.Current.Response.Write("<script>window.open('" + url + "');</script>");
               
                HttpContext.Current.Response.Redirect("/Pages/AccessDeniedOverride.aspx?Error=true&Message=" + ex.Message);
            }

Does this look correct. I can't tell whether I need to update the HttpContext redirect line or the redirect + line.

Anyone with any ideas on this please.

where you want you use pop-up use response.write instead of redirect

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.