地址重写后,来一个按钮事件,页面地址马上返回原形,解决这个问题的方法是:
using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Web.UI;
namespace GB
{
/// <summary>
///NewForm 的摘要说明
/// </summary>
public class NewForm : Page
{
public NewForm()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
protected override void Render(HtmlTextWriter writer)
{
if (writer is System.Web.UI.Html32TextWriter)
{
writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
}
else
{
writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
}
base.Render(writer);
}
}
internal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter
{
private string _url; // 假的URL
internal FormFixerHtml32TextWriter(TextWriter writer)
: base(writer)
{
_url = HttpContext.Current.Request.RawUrl;
}
public override void WriteAttribute(string name, string value, bool encode)
{
if (_url != null && string.Compare(name, "action", true) == 0)
{
value = _url;
}
base.WriteAttribute(name, value, encode);
}
}
internal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter
{
private string _url;
internal FormFixerHtmlTextWriter(TextWriter writer)
: base(writer)
{
_url = HttpContext.Current.Request.RawUrl;
}
public override void WriteAttribute(string name, string value, bool encode)
{
if (_url != null && string.Compare(name, "action", true) == 0)
{
value = _url;
}
base.WriteAttribute(name, value, encode);
}
}
}
定义上面的类,在有使用页面按钮事件的页面,
public partial class Default: System.Web.UI.Page
改为:
public partial class Default: GB.NewForm