更改的HtmlForm行动在C#ASP.NET 3.5 [英] Change HtmlForm action in C# ASP.NET 3.5

查看:119
本文介绍了更改的HtmlForm行动在C#ASP.NET 3.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形式

<form id="form" action="" method="post" runat="server">

当通过

HtmlForm form = (HtmlForm)this.FindControl("form");

和尝试与改变行动

form.Attributes.Add("action","./newpage.aspx?data=data");

form.Attributes["action"] = "./newpage.aspx?data=data");

不进行任何更改。该表格​​还路由到同一页面。我怎样才能动态改变形式的行动,codebehind?

no change is made. The form still routes to the same page. How can I dynamically change the form's action in codebehind?

额外的细节:
我有一个具有GET变量的页面。这得到了形式的作用部分被发送变量的需求。因此,第1页响应具有getvar1。第一页上表格需要发送的数据后,并getvar1。我正想通过code-落后于形式的作用来调节这一点,但要避免使用innerHTML来写整个表单。霍利建议的JavaScript,但我还没有找到让GET瓦尔使用javascript的好方法。 .....群众只是更多的信息。

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.

答案解释:我选择去了@HollyStyles提到的路线。我用JavaScript来改变形式的行动Ajax调用结束后。然而,答案正确标注是通过做这个正确的方法code-落后。

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.

推荐答案

您可以使用控制适配器 asp.net的。

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);
    }
}

通过上面的code,并在声明 App_Browsers文件有一个名为 Form.browser

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>

您可以改变形式。当然,所谓的每一个构成本code渲染。

you can change the form. Of course this code called in every form render.

相对<一个href=\"http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx\">http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

这篇关于更改的HtmlForm行动在C#ASP.NET 3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆