0
我在Default.aspx中有兩個UserControls。如何從UserControl調用方法到另一個UserControl或另一個aspx.Page使用事件?
我想要做如下:
如果我按下按鈕(SET按鈕),那麼我想如果選擇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;
}
}
頁面是兩個UserControls的控制器。所以它負責顯示/加載它們並處理事件。因此你應該創建一個你在UC1中提出的自定義事件,在'Page'中處理。然後頁面調用UC2中的方法/屬性。 –