使用Reflection的自定义验证器动态ServerValidate

时间:2010-07-04 19:56:31

标签: asp.net reflection customvalidator

我有一个UserControl,它包含一个TextBox和一个CustomValidator。

我想将CustomValidator.ServerValidate设置为包含UserControl的页面中的方法

我发现这段代码可以让我动态设置自定义验证器验证功能:

cusvCustom.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(MethodName);

问题是字符串值在那里不起作用。它需要是该方法的参考。是否可以使用反射(或其他方法)仅使用其字符串名称来获取对父控件方法的有效引用?我想使用方法名称的字符串值的原因是我可以将控件放在页面上:

<uc1:TextBoxField ID="tbUserName" runat="server" CustomValidationMethod="ValidateUserName" />

我做了一些研究,发现了Type.GetMethod和MethodInfo,但我无法让它们起作用。主要是因为我不知道父控件的类型,也无法弄清楚如何获得它。

编辑:我的matt-dot-net代码

WebUserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl.ascx.cs" Inherits="WebApplication1.WebUserControl" %>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Custom Validation Failed" OnServerValidate="CustomValidator1_ServerValidate" />
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Submit" CausesValidation="true" />

WebUsecControl.ascx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebUserControl : System.Web.UI.UserControl
    {
        public ServerValidateEventHandler Validating;

        protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
        {
            if (Validating != null)
                Validating(sender, e);
        }
    }
}

TestPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="WebApplication1.TestPage" %>
<%@ Register Src="~/WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <uc1:WebUserControl ID="WebUserControl1" runat="server" OnValidating="WebUserControl1_Validating" />

    </div>
    </form>
</body>
</html>

TestPage.aspx.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class TestPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //WebUserControl1.Validating += WebUserControl1_Validating;
        }

        protected void WebUserControl1_Validating(Object sender, ServerValidateEventArgs e)
        {
            e.IsValid = false;
        }
    }
}

正如您所看到的,它几乎与您的代码完全相同。无论出于什么原因,它对我不起作用,因为我在这里。当我点击按钮时页面重新加载并且是相同的。当我取消注释一行并单击按钮时,我会看到错误消息。

1 个答案:

答案 0 :(得分:2)

我认为这就是你想要做的事情:

WebUserControl.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Custom Validation Failed"
ControlToValidate="TextBox1" OnServerValidate="CustomValidator1_ServerValidate" />
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Submit" CausesValidation="true" />

在您的代码中:

public partial class WebUserControl : System.Web.UI.UserControl
{
     public event ServerValidateEventHandler Validating;

     protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs e)
     {
         if (Validating != null)
             Validating(sender, e);
     }
 }

这将允许您以您希望的方式在页面上使用控件:

<uc1:WebUserControl ID="WebUserControl1" runat="server" OnValidating="WebUserControl1_Validating" />

然后在你的.aspx.cs代码中:

protected void WebUserControl1_Validating(Object sender, ServerValidateEventArgs e)
{
   //do validation here
}
相关问题