ReturnURL ignoring it



If you need to force the user to a particular page, regardless of what the ReturnURL is stating, then use the following code in your Page_Load


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !string.IsNullOrEmpty(Request.QueryString["ReturnURL"]))
            {
                //Do this to remove any URL info the user may be trying to get to
                Response.Redirect(@"default.aspx");
            }

        }

VS2005 ASP.Net not stopping on breakpoints

If you have IE 8 installed and your ASP.Net project won’t stop on the breakpoints you’ve set they you probably need to read this article

You basically need to add an entry to your registry to stop process growth for tabs.

I’ve copied the steps required below

1) Open RegEdit
2) Browse to HKEY_LOCALMACHINE -> SOFTWARE -> Microsoft -> Internet Explorer -> Main
3) Add a dword under this key called TabProcGrowth
4) Set TabProcGrowth to 0

Web.Config allow access to certain paths

If you’ve locked down access to your website but what your login page to pick up a stylesheet from a sub folder then you need to a add a location tag to your web.config to grant access

<location path="Styles">
    <system.web>
        <authorization>
            <allow users="*" />
        </authorization>
    </system.web>
</location>
<location path="Images">
    <system.web>
        <authorization>
            <allow users="*" />
        </authorization>
    </system.web>
</location>

Specify file name for file from handler

When returning content to a user from a generic handler use the the AddHeader method to specify the filename

context.Response.ContentType = "text/comma-separated-values";
context.Response.AddHeader("content-disposition", "attachment; filename=PrinterDirect.CSV");

Creating Event Handler Signatures

In Visual Studio 2008 you’ve lost the abillity to click on the drop down in the code behind and see all the event signatures that are available but haven’t handled yet. To get the signature you are after without using the properites in the desinger you can type…


this.

As you hit the . all methods and events should appear, pick the event you after e.g. Load

Then add type += after the event name and press TAB TAB, the event is automatically generated. ASP.Net auto wires the events with the correct name and signature so you should be able to delete the line of code that binds the event.

Check out this article for more info.

Server Control Remove Span

Normally when you write a server control, .Net automatically wraps the output with a span and the Id of the control. Depending on your CSS this can cause rendering issues. You can use the following technique to prevent the span from being rendered.

public override void RenderBeginTag(HtmlTextWriter writer)
{
    using (HtmlTextWriter blankOne = new HtmlTextWriter(new System.IO.StringWriter()))
    {
        base.RenderBeginTag(blankOne);
    }
}
public override void RenderEndTag(HtmlTextWriter writer)
{
}

It basically writes out the open tag to a new HTML Writer and therefore is not part of the final rendered HTML

SessionId – keeping it the same

If you don’t populate a value in the session of a page then the session Id will reset everytime the page is loaded.

protected void Page_PreInit(object sender, EventArgs e)
{
    //Need this line otherwise it resets the Session Id everytime!
    Session["keepme"] = "True";
}

HTML Codes

The following article lists all of the HTML codes that can be used in a web page to represent special characters.

Render Image using Generic Handler ASP.Net

The following code uses a property from an object that returns a byte array that contains an image.

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "image/jpg";
    if (context.Request.Params["Id"] != null)
    {
        Content selectedImage = new Content(Convert.ToInt32(context.Request.Params["Id"].ToString()));
        if (context.Request.Params["t"] == "D")
        {
            context.Response.OutputStream.Write(selectedImage.ImageDetails.FullImage, 0, selectedImage.ImageDetails.FullImage.Length);
        }
        else
        {
            context.Response.OutputStream.Write(selectedImage.ImageDetails.ThumbnailImage, 0, selectedImage.ImageDetails.ThumbnailImage.Length);
        }
    }
}

Download content rather than view it in a Browser

Add the following line to ensure the user is prompted to download a file

Response.AddHeader "content-disposition","attachment; filename=fname.ext"

Previous Entries