0

我有一个现有的项目,所以我需要一种可靠的方法来全局清理/检查用户输入。

我有一个带有form标签的母版页(父),“子”页包含带有回发命令的表单元素,插入/更新/删除到数据库等。

仅使用 C# - 是否有某种方法可以在帖子通过子页面运行之前全局拦截回发?

是否可以检查它是否是文本框/复选框/单选按钮等以不同方式操作用户输入的清理?

4

3 回答 3

1

这就是我为使其正常工作所做的。

  • 覆盖我的 MasterPage 上的 OnInit 事件

  • 为我的 Request 集合禁用 ReadOnly 以便它是可设置的。

  • 循环通过 Posted .Net 控件,清理值并设置新值。

  • 重新启用只读。

在全球范围内工作得很好,为我节省了几天的体力劳动:)

protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

        if (IsPostBack)
        {
            var fCollection = (NameValueCollection)Request.GetType().GetField("_form", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request);
            PropertyInfo oReadable = fCollection.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
            oReadable.SetValue(fCollection, false, null);

            foreach (string s in Request.Params.Keys)
            {
                var pCtrl = Page.FindControl(s);

                //Sanitize the posted values (TextHandler.SimpleSanitize is my own static method)
                if (pCtrl != null) fCollection[s] = TextHandler.SimpleSanitize(fCollection[s]);
            }

            oReadable.SetValue(fCollection, true, null);

        }
    }
于 2012-06-26T09:44:21.523 回答
0

您可以使用处理 context.BeginRequest 事件的自定义 IHttpModule 来做一些事情。

public class MyHttpModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(OnBeginRequest);
    }

    protected void OnBeginRequest(object sender, EventArgs e)
    {
        var context = ((HttpApplication) sender).Context;
        // do something here, use the context to get request info
    }

    public void Dispose()
    {
    }
}

您必须在 web.config 中注册 HttpModule,例如:

<httpModules>
  <add name="MyHttpModule" type="MyProject.MyNamespace.MyHttpModule, MyProject"/>
</httpModules>
于 2012-06-21T13:34:42.210 回答
-1

只需在您的 MasterPage 标记中添加 Jquery 函数,它将拦截您需要的所有事件并执行您需要的操作。为了捕获只需要控件,您可以应用 css 类

于 2012-06-21T12:18:08.300 回答