Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Aspx中指定C#Lambda Func_C#_Asp.net_Delegates_Func - Fatal编程技术网

在Aspx中指定C#Lambda Func

在Aspx中指定C#Lambda Func,c#,asp.net,delegates,func,C#,Asp.net,Delegates,Func,我有一个自定义控件,我想在其中将方法公开为属性(例如,用于自定义验证) 这是有效的 但是,我认为在aspx中这样做更方便,如: <uc1:MatrixTable ID="ucMatrixTable" runat="server" ValidateMatrixFunc="ValidateMatrix" /> 但这会导致以下消息崩溃: 无法从'ValidateMatrixFunc'属性的字符串表示形式'ValidateMatrix'创建'System.Func'1[[System.

我有一个自定义控件,我想在其中将方法公开为属性(例如,用于自定义验证)

这是有效的

但是,我认为在aspx中这样做更方便,如:

<uc1:MatrixTable ID="ucMatrixTable" runat="server" ValidateMatrixFunc="ValidateMatrix" />

但这会导致以下消息崩溃:

无法从'ValidateMatrixFunc'属性的字符串表示形式'ValidateMatrix'创建'System.Func'1[[System.Boolean,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089]]类型的对象


所以,我只是想知道。。。我想知道。。如果某个忍者知道这个问题的答案,或者这只是我们永远无法理解的生命奥秘之一。

它将函数名解释为一个文字。尝试使用数据绑定表达式

您需要使用它,但这里有一个可能的示例:

<uc1:MatrixTable ID="ucMatrixTable" runat="server" ValidateMatrixFunc="<%# ValidateMatrix %>" />

ASP.NET用于转换通过标记指定的属性值的
字符串
表示形式,以将
字符串
转换为属性的正确类型。错误是告诉您没有为
Func
类型注册的
TypeConverter
TypeConverter
必须在类本身上注册,因此这不是您可以做的事情,而且无论如何,我认为它不会让您实现您想要的目标。

这种方法不起作用,但您的基本目标非常普遍:。有关更多信息,请参阅本MSDN文章:

您的“return”bool应该在事件处理程序中实现:

public void GetCurrentRecords(object sender, EventArgs e)
{
    var obj  = sender as Lcmp.Website.Data.Controls.AFTO95ViewerControl;
    obj.ValidateMatrixFunc = whatever;
}

您可能希望将“ValidateMatrixFunc”属性作为事件公开。为什么?它更符合控件通常的实现方式。此外,事件允许您为单个事件拥有多个订阅者(事件处理程序)。虽然这可能不是一个典型的用例,但有时确实会发生

我已经在下面描述了如何将其作为一项活动来实施:

让我们将该事件称为“ValidatingMatrix”

然后您可以这样编写您的ASPX标记:

<uc1:MatrixTable ID="ucMatrixTable" runat="server" OnValidatingMatrix="ValidateMatrix" />
protected void ValidateMatrix(object sender, System.ComponentModel.CancelEventArgs e)
{
    // perform validation logic

    if (validationFailed)
    {
        e.Cancel = true;
    }
}
    const string ValidatingMatrixEventKey = "ValidatingMatrix";

    public event System.ComponentModel.CancelEventHandler ValidatingMatrix
    {
        add { this.Events.AddHandler(ValidatingMatrixEventKey, value); }
        remove { this.Events.RemoveHandler(ValidatingMatrixEventKey, value); }
    }

    protected bool OnValidatingMatrix()
    {
        var handler = this.Events[ValidatingMatrixEventKey] as System.ComponentModel.CancelEventHandler;
        if (handler != null)
        {
            // prepare event args
            var e = new System.ComponentModel.CancelEventArgs(false);

            // call the event handlers (an event can have multiple event handlers)
            handler(this, e);

            // if any event handler changed the Cancel property to true, then validation failed (return false)
            return !e.Cancel;
        }

        // there were no event handlers, so validation passes by default (return true)
        return true;
    }

    private void MyLogic()
    {
        if (this.OnValidatingMatrix())
        {
            // validation passed
        }
        else
        {
            // validation failed
        }
    }
在MatrixTable自定义控件中,实现如下内容:

<uc1:MatrixTable ID="ucMatrixTable" runat="server" OnValidatingMatrix="ValidateMatrix" />
protected void ValidateMatrix(object sender, System.ComponentModel.CancelEventArgs e)
{
    // perform validation logic

    if (validationFailed)
    {
        e.Cancel = true;
    }
}
    const string ValidatingMatrixEventKey = "ValidatingMatrix";

    public event System.ComponentModel.CancelEventHandler ValidatingMatrix
    {
        add { this.Events.AddHandler(ValidatingMatrixEventKey, value); }
        remove { this.Events.RemoveHandler(ValidatingMatrixEventKey, value); }
    }

    protected bool OnValidatingMatrix()
    {
        var handler = this.Events[ValidatingMatrixEventKey] as System.ComponentModel.CancelEventHandler;
        if (handler != null)
        {
            // prepare event args
            var e = new System.ComponentModel.CancelEventArgs(false);

            // call the event handlers (an event can have multiple event handlers)
            handler(this, e);

            // if any event handler changed the Cancel property to true, then validation failed (return false)
            return !e.Cancel;
        }

        // there were no event handlers, so validation passes by default (return true)
        return true;
    }

    private void MyLogic()
    {
        if (this.OnValidatingMatrix())
        {
            // validation passed
        }
        else
        {
            // validation failed
        }
    }

非常感谢。它将函数名解释为文字“duhh。。但是我也无法让绑定在aspx上工作。Func和event在概念上是不同的。事件在某个事件发生时引发,而
func
predicate
可用于筛选出某个事件。使用事件进行过滤是不自然或不恰当的。诚然,在OP的例子中,事件听起来更好。