I have a form as
<form id="form" action="" method="post" runat="server">
When accessing in C# code-behind via
HtmlForm form = (HtmlForm)this.FindControl("form");
and attempting to change the action with
form.Attributes.Add("action","./newpage.aspx?data=data");
or
form.Attributes["action"] = "./newpage.aspx?data=data");
no change is made. The form still routes to the same page. How can I dynamically change the form's action in codebehind?
EXTRA DETAILS: I have a page that has a get variable. That get variable needs to be sent in the action portion of the form. So, page1 response has getvar1. The form on page1 needs to send its post data and getvar1. I was going to adjust this via code-behind in the action of the form, but wanted to avoid using InnerHtml to write the whole form. Holly suggested javascript, but I haven't found a good way of getting GET vars with javascript. ..... just more information for the masses.
ANSWER EXPLANATION: I chose to go the route that @HollyStyles mentioned. I used javascript to change the form action after the ajax call completed. However, the answer marked correct is the right way to do this via code-behind.
The action attribute defines the action to be performed when the form is submitted. Usually, the form data is sent to a file on the server when the user clicks on the submit button. In the example below, the form data is sent to a file called "action_page.php".
I found that changing the form's action caused example.com/index.pl (with no page parameter) to be rendered, even though the expected URL ( example.com/page-name ) was displayed in the address bar. To get around this, I used JavaScript to insert a hidden field to set the page parameter.
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.
You can use the Control Adapters of asp.net.
Here is a working example:
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);
}
}
With the above code, and a declare on the App_Browsers
with a file called Form.browser
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="FormRewriterControlAdapter" />
</controlAdapters>
</browser>
</browsers>
you can change the form. Of course this code called in every form render.
Relative : http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
You can change the form action like this:
protected void Page_Init(object sender, EventArgs e)
{
Form.Attributes.Add("action", "/Registration/Signup.aspx");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With