Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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
Asp.net 从用户控件调用父级中的方法_Asp.net_Vb.net_User Controls - Fatal编程技术网

Asp.net 从用户控件调用父级中的方法

Asp.net 从用户控件调用父级中的方法,asp.net,vb.net,user-controls,Asp.net,Vb.net,User Controls,出于一些与业务相关的原因,我制作了一个向导导航用户控件。非常简单:下一个/上一个/完成按钮 使用此nav用户控件的每个父页面都有一个SaveData函数,该函数保存数据并执行一系列其他操作 当用户单击Next/Previous/Finish时,我想在重定向到其他页面之前调用父级的SaveData函数。我应该怎么做,或者你能推荐一个更好的方法(如果可能的话请提供代码示例) 提前谢谢 UserControl不应调用Parent的函数。这不是一个好的设计 相反,您希望将事件从UserControl冒泡

出于一些与业务相关的原因,我制作了一个向导导航用户控件。非常简单:下一个/上一个/完成按钮

使用此nav用户控件的每个父页面都有一个SaveData函数,该函数保存数据并执行一系列其他操作

当用户单击Next/Previous/Finish时,我想在重定向到其他页面之前调用父级的SaveData函数。我应该怎么做,或者你能推荐一个更好的方法(如果可能的话请提供代码示例)


提前谢谢

UserControl不应调用Parent的函数。这不是一个好的设计

相反,您希望将事件从UserControl冒泡到父页面

注意:我的项目的名称空间是DemoWebForm

MyUserControl.ascx Parent.aspx
UserControl不应调用父项的函数。这不是一个好的设计

相反,您希望将事件从UserControl冒泡到父页面

注意:我的项目的名称空间是DemoWebForm

MyUserControl.ascx Parent.aspx
您只需要委托人:

public delegate void ClickEventHandler(object sender, EventArgs e);
public event ClickEventHandler NextClicked; 

您只需要委托人:

public delegate void ClickEventHandler(object sender, EventArgs e);
public event ClickEventHandler NextClicked; 

可能最好不要直接调用它,这会使用户控件不可移植。相反,在控件上有一个页面可以订阅的事件。例如,您的控件有一个
OnSave
事件,因此当您在页面上声明控件时,您可能会执行类似
的操作,最好不要直接调用它,这将使用户控件不可移植。相反,在控件上有一个页面可以订阅的事件。例如,您的控件有一个
OnSave
事件,因此当您在页面上声明控件时,您可能会执行类似
+1的操作,这是一个很好的示例!但是这个问题在VB.NET中被标记了。如果你知道VB,也许你可以改用VB?我会为你做的,但我只能读VB,不能写。你可以用。对不起,我已经有几年没有写VB了。在Parent.aspx中,是否应该下一次单击OnNextClicked?(其他人也一样)我尝试使用您的示例,对于ClickEventHandler,我收到“找不到类型或命名空间'ClickEventHandler'是否缺少using指令…”。ClickEventHandler的使用指令是什么?@DovMiller您可以创建一个新问题,并在这里发布一个链接吗?我很乐意看一看。+1个很好的例子!但是这个问题在VB.NET中被标记了。如果你知道VB,也许你可以改用VB?我会为你做的,但我只能读VB,不能写。你可以用。对不起,我已经有几年没有写VB了。在Parent.aspx中,是否应该下一次单击OnNextClicked?(其他人也一样)我尝试使用您的示例,对于ClickEventHandler,我收到“找不到类型或命名空间'ClickEventHandler'是否缺少using指令…”。ClickEventHandler的使用指令是什么?@DovMiller您可以创建一个新问题,并在这里发布一个链接吗?我很乐意看一看。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Parent.aspx.cs" 
    Inherits="DemoWebForm.Parent" %>

<%@ Register Src="MyUserControl.ascx" TagName="MyUserControl" TagPrefix="uc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <uc1:MyUserControl ID="MyUserControl1" runat="server"
            OnNextClicked="MyUserControl1_NextClicked"
            OnPreviousClicked="MyUserControl1_PreviousClicked"
            OnFinishClicked="MyUserControl1_FinishClicked" />
        <asp:Literal runat="server" ID="MessageLiteral" />
    </form>
</body>
</html>
public partial class Parent : System.Web.UI.Page
{
    protected void MyUserControl1_NextClicked(object sender,EventArgs e)
    {
        MessageLiteral.Text = "Next button was clicked";
    }

    protected void MyUserControl1_PreviousClicked(object sender, EventArgs e)
    {
        MessageLiteral.Text = "Previous button was clicked";
    }

    protected void MyUserControl1_FinishClicked(object sender, EventArgs e)
    {
        MessageLiteral.Text = "Finish button was clicked";
    }
}
public delegate void ClickEventHandler(object sender, EventArgs e);
public event ClickEventHandler NextClicked;