2010-07-04 40 views
0

我有一个UserControl,它包含一个TextBox和一个CustomValidator。使用反射的自定义验证器动态ServerValidate

我想给CustomValidator.ServerValidate设置为包含用户控件

我发现这个代码,这将让我动态设置页面的方法的自定义验证验证功能:

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

问题是,字符串值不会在那里工作。它需要是对该方法的参考。是否有可能使用反射(或其他一些方法)只使用它的字符串名称来获取父控件方法的有效引用?我想使用的方法名的字符串值的原因是这样我就可以放置控件的页面上正是如此:

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

我做了一些研究,我发现Type.GetMethod和MethodInfo的,但我不能让他们去工作。主要是因为我不知道父控件的类型,不知道如何获取它。

编辑:我的亚光点网

代码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; 
     } 
    } 
} 

正如你所看到的,它几乎是你的代码的完全重复。无论出于何种原因,它在我这里都不适合我。当我点击按钮时,页面重新加载并且是相同的。当我通过取消注释一行并单击按钮时,我看到错误消息。

回答

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 
} 
+0

其实,这正是我要找的,比我怎么想它会处理更加简单。我现在唯一的问题是它不工作。 CustomValidator1_ServerValidate被调用,但验证始终为空。你有什么想法,为什么?在intellisense中,OnValidating也会为您显示吗?这不适合我。 – William 2010-07-04 22:10:57

+0

如果您尚未在页面代码中连接事件处理程序,则验证将为空。 – 2010-07-05 00:26:39

+0

嗯。对我来说仍然很奇怪。我精确地复制了你的代码,并把“e.IsValid = false;”作为我的验证,所以它应该总是失败。我不知道为什么,但它仍然不适用于我,除非我添加“WebUserControl1.Validating + = WebUserControl1_Validating;”到aspx页面的Page_Load方法。我想它已经完成了这项工作,但如果可能的话,不必增加页面加载方法也不失为一件好事。 – William 2010-07-05 02:32:49