如何使用Events将UserControl中的方法调用到另一个UserControl或另一个aspx.Page?

时间:2013-01-19 16:29:27

标签: c# asp.net events user-controls

我在Default.aspx中有两个UserControl。

我想做以下事情:

如果我按下按钮(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;
        }

    }

1 个答案:

答案 0 :(得分:2)

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

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