FormRewriterControlAdapter.cs
然后在 App_Browsers 文件夹添加 BrowserFile.browser 浏览器文件
在里面添加以下一段代码
这里利用新的 ASP.NET 2.0控件适配器扩展架构来定制控件的输出,用你提供的值来覆盖action属性的值
不需要自己重写页面代码
II原来的解决方法
public
class
FormRewriterControlAdapter:System.Web.UI.Adapters.ControlAdapter
{
public FormRewriterControlAdapter()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected override void Render(HtmlTextWriter writer)
{
base .Render( new RewriteFormHtmlTextWriter(writer));
}
}
public class RewriteFormHtmlTextWriter:HtmlTextWriter
{
/// <summary>
/// 调用基类的构造方法
/// </summary>
/// <param name="writer"></param>
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 " )
{
HttpContext context = HttpContext.Current;
if (context.Items[ " ActionAlreadyWritten " ] == null )
{
value = context.Request.RawUrl;
context.Items[ " ActionAlreadyWritten " ] = true ;
}
}
base .WriteAttribute(name, value, fEncode);
}
}
{
public FormRewriterControlAdapter()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
protected override void Render(HtmlTextWriter writer)
{
base .Render( new RewriteFormHtmlTextWriter(writer));
}
}
public class RewriteFormHtmlTextWriter:HtmlTextWriter
{
/// <summary>
/// 调用基类的构造方法
/// </summary>
/// <param name="writer"></param>
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 " )
{
HttpContext context = HttpContext.Current;
if (context.Items[ " ActionAlreadyWritten " ] == null )
{
value = context.Request.RawUrl;
context.Items[ " ActionAlreadyWritten " ] = true ;
}
}
base .WriteAttribute(name, value, fEncode);
}
}
然后在 App_Browsers 文件夹添加 BrowserFile.browser 浏览器文件
在里面添加以下一段代码
<
browser
refID
="Default"
>
< controlAdapters >
< adapter controlType ="System.Web.UI.HtmlControls.HtmlForm"
adapterType ="FormRewriterControlAdapter" />
</ controlAdapters >
</ browser >
< controlAdapters >
< adapter controlType ="System.Web.UI.HtmlControls.HtmlForm"
adapterType ="FormRewriterControlAdapter" />
</ controlAdapters >
</ browser >
这里利用新的 ASP.NET 2.0控件适配器扩展架构来定制控件的输出,用你提供的值来覆盖action属性的值
不需要自己重写页面代码
II原来的解决方法
namespace
ActionlessForm {
public class Form : System.Web.UI.HtmlControls.HtmlForm
{
protected override void RenderAttributes(HtmlTextWriter writer)
{
writer.WriteAttribute( " name " , this .Name);
base .Attributes.Remove( " name " );
writer.WriteAttribute( " method " , this .Method);
base .Attributes.Remove( " method " );
this .Attributes.Render(writer);
base .Attributes.Remove( " action " );
if ( base .ID != null )
writer.WriteAttribute( " id " , base .ClientID);
}
}
}
页面中的 Form 继承该Form 就可以实现相同的目的了
public class Form : System.Web.UI.HtmlControls.HtmlForm
{
protected override void RenderAttributes(HtmlTextWriter writer)
{
writer.WriteAttribute( " name " , this .Name);
base .Attributes.Remove( " name " );
writer.WriteAttribute( " method " , this .Method);
base .Attributes.Remove( " method " );
this .Attributes.Render(writer);
base .Attributes.Remove( " action " );
if ( base .ID != null )
writer.WriteAttribute( " id " , base .ClientID);
}
}
}