Tuesday, January 11, 2011

Open new window with Response.Redirect() in Asp.net- 3rd part

In 2nd part of the following post I show you how to open a new window in asp.net using "ResponseHelper "class. Now today I want to share another approach with "Extension method".

Third approach(Extension method)
public static class ResponseHelper {
    public static void Redirect(this HttpResponse response,
        string url,
        string target,
        string windowFeatures) {
 
        if ((String.IsNullOrEmpty(target) ||
            target.Equals("_self", 
StringComparison
.OrdinalIgnoreCase)) &&
                         String.IsNullOrEmpty(windowFeatures)) {
 
            response.Redirect(url);
        }
        else {
            Page page = (Page)HttpContext.Current.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);
        }
    }
}

Note the 'this' keyword in the first parameter.
Now whenever we include the namespace this class
is defined within, we get a nice override on the
actual Response object.

Ex:
Response.Redirect("popup.aspx", "_blank", "menubar=0,width=100,height=100");

Thursday, January 6, 2011

Open new window with Response.Redirect() in Asp.net- 2nd part

In my previous post I show you how to open a new window in asp.net using "aspnetForm.target".

Second approach(Using ResponseHelper class)

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 you can just call ResponseHelper.Redirect, first parameter is desired "url", second parameter is for specify a target to be "_self" or "_blank" and third parameter is "windowFeatures" by which you can specify whether the new window should have a menu bar, and what its width and height are.

Disclaimers:

Note: If you use it outside the context of a Page request, you can't redirect to a new window. The reason is the need to call the ResolveClientUrl method on Page, which I can't do if there is no Page. I could have just built my own version of that method, but it's more involved than you might think to do it right. So if you need to use this from an HttpHandler other than a Page, you are on your own.

Note: Beware of popup blockers.

Note: Obviously when you are redirecting to a new window, the current window will still be hanging around. Normally redirects abort the current request -- no further processing occurs. But for these redirects, processing continues, since we still have to serve the response for the current window (which also happens to contain the script to open the new window, so it is important that it completes).

Tuesday, December 28, 2010

Open new window with Response.Redirect() in Asp.net

Today I want share with you a very interesting tips using Response.Redirect(). In asp.net there are various ways to move from one page to another. Mostly used.
  1. Response.Redirect()
  2. Server.Transfer()
 But unfortunately you cant open a new window with those two methods. Which is possible with anchor link like <a href="#" target="_blank" >. When goggling I find that most of the answer is it is not possible or to make it possible you would add JavaScript method to using it. But I don't want this, at last I found two approaches. Now I want to share with these two approaches with asp.net  Response.Redirect() and  Server.Transfer().

First approach(easiest way):

Just use the OnClientClick event of the button. 
<asp:Button ID="btnHome" runat="Server" CssClass="button" Text="Go home"
OnClick="btnHome_Click" OnClientClick="aspnetForm.target ='_blank';"/>
protected void btnHome_Click(object sender, EventArgs e)
{
    Response.Redirect("Home.aspx");
}

If you want to access the form inside the Master page to Child page level

Button1.OnClientClick=string.Format("{0}.target='_blank';",((HtmlForm)Page.Master.FindControl("form1")).ClientID);

This code will find the controls collection and find your form and allow you to get access to the client id so you can be sure you have proper naming of it for the JavaScript to function correctly. Make sure you replace "form1" with whatever you have your parent form id="name" set to inside your Master’s page markup.

For multiple buttons on a single page and only want a specific button to launch into a new window.

<asp:Button ID="btnHomePage" runat="Server" CssClass="button" Text="Your home"
OnClick="btnHomePage_Click" OnClientClick="aspnetForm.target ='_self';"/>

protected void btnHomePage_Click(object sender, EventArgs e)
{
   //Do normal code for postback
}

For more details.visits

In next post I will share with you another approach to open new window using response.redirect() insha Allah.

Presentation @ Microsoft Talkz in AIUB

Yesterday I had joined a nice seminar at AIUB(American International University Bangladesh) arranged by Microsoft which is titled by "Microsoft Talkz". I amazed to see the interest of students at that seminar. Huge crowd in  the auditorium some students were waiting outside the hall for entrance but no single space is available for accommodate them.Really a new experience for me. I feel lucky to get the opportunity to participate that event.

Microsoft student partner(MSP) did a very good job to organize the whole event very nicely. There were also some expert technical guys discuss with new Microsoft technologies.

Razor new view engine in asp.net was my presentation topic. One can download it from here: Razor view engine in asp.net .

Monday, December 6, 2010

Presentation@Kuet S-PAC Programm

Very recently IEEE KUET branch organized a awesome program Students Professional Awareness Conference(S-PAC ).  IEEE Student Branch KUET link( www.kuet.ac.bd/ieee ) introducing First S-PAC on Bangladesh Dated 3,4,5 December,2010 on KUET campus.

Students Professional Awareness Conference(S-PAC ) covers Workshop, Seminar, and Panel Discussions & Various Contests on the above stated topics comprising national & international guests.


It's really great initiative by KUET IEEE branch specially a big thanks to Shihab(who is chair for this event) and his team and also the KUET authority. I am also feeling lucky got an opportunity to attend this event as a speaker. My topic is "How to improve security and performance in ASP.net".
One can download it from here: How to improve security and performance in ASP.net

Its really new experience for me. Huge no of audience(almost 3000) were attends this event. I'm  amazed to got huge response from audience after presentation.

Sunday, November 7, 2010

How to add Google Map in asp.net

Today I have added Google Map my asp.net application successfully !! Don't worry it's not a big deal. Very easy and you will find a lots of article, tutorial about it. For using google map you just need a google account for google map api and basic JavaScript knowledge. For non commercial use google map is free.

In aspx page add Google Map API KEY in header part of html....like this
<head >
<script src="http://maps.google.com/maps?file=api&v=2& key=ABQIAAAAclK0B2lXQwV5lPy1rLiTFBSN1aiKepvDswXjKa4j2DDWdYvOjhQMO1tywqS8ObgP5dtO70AyyArhzA"
type="text/javascript"> </script >
</head>
You can get your own google map api key from this link. You can place your url

Monday, September 27, 2010

“Razor” – a new view engine for ASP.NET

Razor” – a new view engine for ASP.NET developed by Microsoft.

The new view-engine option have been working on is optimized around HTML generation using a code-focused templating approach. The codename for this new view engine is “Razor”,

Microsoft had a number of design goals when building and testing Razor, attempting to create a compact, expressive, and fluid ASP.NET view engine.

In addition, the Redmond company worked to ensure that customers would be able to leverage Razor immediately, by using their existing language and HTML skills.

Design Goals for "Razor"

Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'

Exception: Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'...