Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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
C# 如何在用户控件的xml标记中(而不是在代码隐藏中)指定委托或事件处理程序?_C#_Asp.net_Webforms_User Controls - Fatal编程技术网

C# 如何在用户控件的xml标记中(而不是在代码隐藏中)指定委托或事件处理程序?

C# 如何在用户控件的xml标记中(而不是在代码隐藏中)指定委托或事件处理程序?,c#,asp.net,webforms,user-controls,C#,Asp.net,Webforms,User Controls,我尝试过使用XML标记,但无法使其正常工作。如果我能在我的用户控件中定义我自己的回调委托(包含该委托的页面将对其进行设置)以及我的用户控件随后的回调,那就太好了。通常,这是在包含页面的.CS文件中完成的,这样就完成了 但是,当我尝试在XML标记中设置委托时,它并没有按我所希望的那样工作。例如: 在我的Default.aspx中,我想嵌入MyUserContro.ascx,如下所示 public delegate void NotifyDel(string msg); public NotifyD

我尝试过使用XML标记,但无法使其正常工作。如果我能在我的用户控件中定义我自己的回调委托(包含该委托的页面将对其进行设置)以及我的用户控件随后的回调,那就太好了。通常,这是在包含页面的.CS文件中完成的,这样就完成了

但是,当我尝试在XML标记中设置委托时,它并没有按我所希望的那样工作。例如:

在我的Default.aspx中,我想嵌入MyUserContro.ascx,如下所示

public delegate void NotifyDel(string msg);
public NotifyDel OnNotify{set;get;}
public void Page_Load(object sender, EventArg e){
   Notify?.Invoke();
}
在我的default.aspx标记中,我希望这样做,而不是在.CS代码中这样做:

<uc1:MyUserControl runat="server" OnNotify="MyHandler" />

我遇到了一些错误,无法从字符串表示形式转换为委托或类似的效果


非常感谢

我不确定您提供的代码示例是否100%正确,但以下内容应该可以使用:

用户控制代码:

public delegate void NotifyDel(string msg);

public event NotifyDel Notify;

protected void Page_Load(object sender, EventArgs e)
{
    Notify?.Invoke("msg");
}
<uc1:MyUserControl runat="server" id="MyUserControl" OnNotify="MyUserControl_Notify" />
protected void MyUserControl_Notify(string msg)
{
    // ...
}
默认标记:

public delegate void NotifyDel(string msg);

public event NotifyDel Notify;

protected void Page_Load(object sender, EventArgs e)
{
    Notify?.Invoke("msg");
}
<uc1:MyUserControl runat="server" id="MyUserControl" OnNotify="MyUserControl_Notify" />
protected void MyUserControl_Notify(string msg)
{
    // ...
}