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

发布于 2024-09-08 07:46:45 字数 3025 浏览 6 评论 0原文

我有一个 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;
        }
    }
}

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

I have a UserControl which contains a TextBox and a CustomValidator.

I would like to set the CustomValidator.ServerValidate to a method in the page that contains the UserControl

I found this code which will allow me to dynamically set the custom validators validation function:

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

The problem is that a string value won't work there. It needs to be a reference to the method. Is it possible to use reflection (or some other method) to get a valid reference to the parent controls method using only the string name of it? The reason I want to use the string value of the method name is so I can place the control on the page thusly:

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

I did some research and I found Type.GetMethod and MethodInfo but I can't get them to work. Primarily because I don't know the type of the parent control and can't figure out how to get it.

EDIT: My code for 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;
        }
    }
}

As you can see it's almost an exact duplicate of your code. For whatever reason it does not work for me as I have it here. When I click on the button the page reloads and is the same. When I un-comment the one line though and click the button then I see the error message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想挽留 2024-09-15 07:46:45

我认为这就是您想要做的:

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
}

I think this is what you want to do:

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" />

And in your code behind:

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);
     }
 }

This will allow you to use the control on a page the way you want to:

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

Then in your .aspx.cs code behind:

protected void WebUserControl1_Validating(Object sender, ServerValidateEventArgs e)
{
   //do validation here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文