Change HtmlForm action in C # ASP.NET 3.5
I have a form like
<form id="form" action="" method="post" runat="server">
When accessing C # with code
HtmlForm form = (HtmlForm)this.FindControl("form");
and trying to change the action with
form.Attributes.Add("action","./newpage.aspx?data=data");
or
form.Attributes["action"] = "./newpage.aspx?data=data");
no changes are made. The form is still routed to one page. How can I dynamically change the action of a form in codebehind?
ADDITIONAL DETAILS: I have a page with the get variable. This variable must be sent in the action part of the form. So the response of page1 has getvar1. The form on page 1 should submit its data and getvar1. I was about to set this up with code in the action of the form, but would like to avoid using InnerHtml to write the entire form. Holly offered javascript, but I did not find a good way to get GET vars with javascript ...... just more information for the masses.
ANSWER: , @HollyStyles. javascript ajax-. - .
Control Adapters asp.net.
:
public class RewriteFormHtmlTextWriter : HtmlTextWriter
{
public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
: base(writer)
{
this.InnerWriter = writer.InnerWriter;
}
public RewriteFormHtmlTextWriter(System.IO.TextWriter writer)
: base(writer)
{
base.InnerWriter = writer;
}
public override void WriteAttribute(string name, string value, bool fEncode)
{
if (name == "action")
{
value = "Change here your value"
}
base.WriteAttribute(name, value, fEncode);
}
}
App_Browsers Form.browser
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="FormRewriterControlAdapter" />
</controlAdapters>
</browser>
</browsers>
. , .
: http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
, KB, , , ( : http://www.codeproject.com/Articles/23033/Change-your-ASP-NET-Form-s-Action-attribute-with-R).
, . , SetFormAction(string url) , <form action="url" />.
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
///
/// The purpose of this class is to easily modify the form "action" of a given asp.net page.
/// To modify the action, call the following code in the code-behind of your page (or, better, your MasterPage):
/// Copied (and modified) from http://www.codeproject.com/KB/aspnet/ASP_Net_Form_Action_Attr.aspx
///
public class FormActionModifier : Stream
{
private const string FORM_REGEX = "(]*>)";
private Stream _sink;
private long _position;
string _url;
public FormActionModifier(Stream sink, string url)
{
_sink = sink;
_url = string.Format("$1{0}$3", url);
}
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_REGEX, 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 = string.Concat(s.Substring(0, iform), form, s.Substring(iform + lform));
}
byte[] yaz = System.Text.UTF8Encoding.UTF8.GetBytes(s);
_sink.Write(yaz, 0, yaz.Length);
}
///
/// Sets the Form Action to the url specified
///
public static void SetFormAction(string url)
{
if (HttpContext.Current != null)
HttpContext.Current.Response.Filter = new FormActionModifier(HttpContext.Current.Response.Filter, url);
} // SetFormAction()
} // class
, ASP.NET. . . : http://msdn.microsoft.com/en-us/library/ms178139(v=vs.100).aspx. , , . http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.postbackurl.aspx. , PostBackUrl .
I also had the same issue recently when posting the action attribute of a form would use a rewritten path, but I wanted to show the source url. I don't have url rewrite libraries.
I found that this page is very useful for the operation of my email programs: http://www.codeproject.com/Articles/94335/TIP-How-to-Handle-Form-Postbacks-when-Url-Rewritin