5,664,339 members and growing! (14,481 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto License: The Code Project Open License (CPOL)

Change your asp.net form's action attribute with Response.Filter

By Tolgahan ALBAYRAK

A simple way to change the form's action attribute of an asp.net page with response.filter
C# (C# 2.0, C# 3.0, C#), ASP, ASP.NET, Ajax

Posted: 19 Jan 2008
Updated: 19 Jan 2008
Views: 5,341
Bookmarked: 5 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 3.03 Rating: 4.33 out of 5
1 vote, 20.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
4 votes, 80.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

Sometimes you need to change the asp.net pages form's action attr. Sure that, there are many ways.. And this is one (simple) of them.

Using the code

Just create a class in app_code folder, copy and paste the following lines

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text.RegularExpressions;

public class cdsnetFormActionModifier : Stream
{
    private Stream _sink;
    private long _position;
    string _url;
    public cdsnetFormActionModifier(Stream sink, string url)
    {
        _sink = sink;
        _url = "$1" + url + "$3";
    }

    public override bool CanRead
    {
        get { return true; }
    }

    public override bool CanSeek
    {
        get { return true; }
    }

    public override bool CanWrite
    {
        get { return true; }
    }

    public override long Length
    {
        get { return 0; }
    }

    public override long Position
    {
        get { return _position; }
        set { _position = value; }
    }

    public override long Seek(long offset, System.IO.SeekOrigin direction)
    {
        return _sink.Seek(offset, direction);
    }

    public override void SetLength(long length)
    {
        _sink.SetLength(length);
    }

    public override void Close()
    {
        _sink.Close();
    }

    public override void Flush()
    {
        _sink.Flush();
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return _sink.Read(buffer, offset, count);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        string s = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
        Regex reg = new Regex("(<form.*action=\")([^\"]*)(\"[^>]*>)", RegexOptions.IgnoreCase);
        Match m = reg.Match(s);
        if (m.Success)
        {
            string form = reg.Replace(m.Value, _url);
            int iform = m.Index;
            int lform = m.Length;
            s = s.Substring(0, iform) + form + s.Substring(iform + lform);
        }
        byte[] yaz = System.Text.UTF8Encoding.UTF8.GetBytes(s);
        _sink.Write(yaz, 0, yaz.Length);
    }
}

And configure your load event of any page you want like below
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Response.Filter = new cdsnetFormActionModifier(this.Response.Filter, "your_url");
    }
Please change the string what called "your_url" by the client location of your destination page. That's all Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Tolgahan ALBAYRAK


Happy Coding Smile
vb-forum.net
Occupation: Other
Location: Turkey Turkey

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralNice Ideamemberdinomouse8:21 19 Jan '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Jan 2008
Editor:
Copyright 2008 by Tolgahan ALBAYRAK
Everything else Copyright © CodeProject, 1999-2008
Web09 | Advertise on the Code Project