我在 Default.aspx 中有两个用户控件。
我想做以下事情:
如果我按下按钮(SetButton),那么我想检查 CheckBox1(在 UCCheckBox 中)是否被选中。如果它被选中,那么我从 UCTextBox 调用该方法。该方法称为 HideTextBox。
我如何使用公共事件来做到这一点?
我想从 Default.aspx 调用 UserControls 的方法。
我有这个源代码,例如:
默认.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>
默认.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;
}
}