Tuesday, April 14, 2009

Redirect to New window using Response.Redirect()

Hi,Iam presenting the code to redirect to a new page using our famous response.redirect

Just add this function to .cs file

public static class ResponseHelper
{

public static void Redirect(string url, string target, string windowFeatures)
{
HttpContext context = HttpContext.Current;

if ((String.IsNullOrEmpty(target) ||

target.Equals("_self", StringComparison.OrdinalIgnoreCase)) && String.IsNullOrEmpty(windowFeatures))
{

context.Response.Redirect(url);
}
else
{
Page page = (Page)context.Handler;
if (page == null)
{
throw new InvalidOperationException(
"Cannot redirect to new window outside Page context.");
}
url = page.ResolveClientUrl(url);

string script;

if (!String.IsNullOrEmpty(windowFeatures))
{
script = @"window.open(""{0}"", ""{1}"", ""{2}"");";
}
else
{

script = @"window.open(""{0}"", ""{1}"");";

}

script = String.Format(script, url, target, windowFeatures);
ScriptManager.RegisterStartupScript(page,

typeof(Page),

"Redirect",

script,

true);

}

}

}


now call the function on button click
ResponseHelper.Redirect("popup.aspx", "_blank", "menubar=0,width=100,height=100");

No comments: