简体   繁体   中英

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

I have two UserControls in Default.aspx.

I want to do following:

If I press the button (SetButton) 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.

How can i do this using public events?

I want to call methods from UserControls from Default.aspx.

I have this source code for example:

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;
        }

    }

The button is part of the page so it has visibility of all the controls in the page so why can't you access the control instances like this, presuming your control instance names are UCCheckBox1 and UCTextBox1:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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