2010-01-14 110 views

回答

1

由於NET 2.0(我認爲),你可以手動設置表單的action屬性,以任何你想要的:

<form id="form1" runat="server" action="/"> 

您也可以在代碼隱藏中做到這一點:

form1.Action = "/?" & Request.ServerVariables("QUERY_STRING") 
0

我正在使用這個ControlAdapter修改FORM元素的action屬性到實際的url。它也有用於URL重寫。

public class FormRewriteAdapter : System.Web.UI.Adapters.ControlAdapter 
    { 
     [DebuggerStepThrough()] 
     protected override void Render(HtmlTextWriter writer) 
     { 
      base.Render(new RewriteFormHtmlTextWriter(writer)); 
     } 
    } 


    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")) 
      { 

       System.Web.HttpContext Context = System.Web.HttpContext.Current; 

       if (Context.Items["ActionAlreadyWritten"] == null) 
       { 
        value = Context.Request.RawUrl; 
        Context.Items["ActionAlreadyWritten"] = true; 
       } 
      } 
      base.WriteAttribute(name, value, fEncode); 
     } 

    } 

你必須註冊這個適配器App_Browsers文件目錄是這樣的:

<adapter controlType="System.Web.UI.HtmlControls.HtmlForm" adapterType="MyNamaspace.FormRewriteAdapter" />