2010-07-04 19 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