繁体   English   中英

如何使用事件从UserControl调用方法到另一个UserControl或另一个aspx.Page?

[英]How can i call method from UserControl to another UserControl or another aspx.Page using Events?

我在Default.aspx中有两个UserControls。

我要执行以下操作:

如果按下按钮(SetButton),则要检查是否已选中CheckBox1(在UCCheckBox中)。 如果已选中,则从UCTextBox调用该方法。 该方法称为HideTextBox。

我如何使用公共事件来做到这一点?

我想从Default.aspx从UserControls调用方法。

我有这个源代码,例如:

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="NewControls.Default" %>

<%@ Register Src="~/uc/UCCheckBox.ascx" TagPrefix="uc1" TagName="UCCheckBox" %>
<%@ Register Src="~/uc/UCTextBox.ascx" TagPrefix="uc1" TagName="UCTextBox" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>UserControls</title>
</head>
<body>
    <form id="form1" runat="server">

        <%--UserControl with Checkbox--%>
        <uc1:UCCheckBox runat="server" ID="UCCheckBox" />
        <%--UserControl with Textboxes--%>
        <uc1:UCTextBox runat="server" ID="UCTextBox" />
        <%--Label for result--%>
        <asp:Label ID="ResultLabel" runat="server"></asp:Label>


        <asp:Button ID="SetButton" OnClick="SetButton_Click" runat="server" Text="Button" />

    </form>
</body>
</html>

Default.aspx.cs:

public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SetButton_Click(object sender, EventArgs e)
        {
           //If I press this button then I want to check if the CheckBox1 (in UCCheckBox) is selected. If it´s checked then i call the method from UCTextBox. The method is called HideTextBox.
        }
    }

UCCheckbox.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UCCheckBox.ascx.cs" Inherits="NewControls.uc.UCCheckBox" %>

<asp:CheckBox ID="CheckBox" Text="Checkbox" runat="server" />

UCCheckBoxes.ascx.cs

public partial class UCCheckBox : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public bool isChecked()
        {
            if (CheckBox.Checked)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }

UCTextBox.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UCTextBox.ascx.cs" Inherits="NewControls.uc.UCTextBox" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

UCTextBox.ascx.cs

public partial class UCTextBox : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void hideTextboxes() 
        {
            TextBox1.Visible = false;
            TextBox2.Visible = false;
        }

    }

该按钮是页面的一部分,因此它具有页面中所有控件的可见性,因此,为什么假定控件实例名称为UCCheckBox1和UCTextBox1,您为什么不能访问这样的控件实例:

 protected void SetButton_Click(object sender, EventArgs e)
        {
           if (UCCheckBox1.IsChecked) {
               UCTextBox1.hideTextboxes();
           }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM