0

Default.aspxに2つの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;
        }

    }
4

1 に答える 1

2

ボタンはページの一部であるため、ページ内のすべてのコントロールが表示されるため、コントロールインスタンス名がUCCheckBox1およびUCTextBox1であると仮定して、このようなコントロールインスタンスにアクセスできないのはなぜですか。

 protected void SetButton_Click(object sender, EventArgs e)
        {
           if (UCCheckBox1.IsChecked) {
               UCTextBox1.hideTextboxes();
           }
        }
于 2013-01-19T16:38:27.847 に答える