从.aspx到.ascx的访问控制



我有一个自定义的用户控件,叫做OrderForm.ascx。我也有一个.aspx文件,利用OrderForm控制。

我想从OrderForm控件访问.aspx文件上的控件。有办法做到这一点吗?

您可以像这样在用户控件中使用FindControl方法:

Label label = Page.FindControl("Label1") as Label;
if (label != null)
    string labelText = label.Text;

如上所述,根据Label在页面中的位置,您可能需要使用递归来查找Label。

还可以在页面上创建一个属性,返回Label的文本:

public string LabelText
{
    get { return Label1.Text; }
}
要从用户控件访问属性,有两个选项:

选项# 1

string labelText = ((PageName)Page).LabelText;

选项# 2

string labelText = Page.GetType().GetProperty("LabelText").GetValue(Page, null).ToString();

如果您有两个用户控件,ControlA和ControlB,并且它们都注册在同一页面上,您可以轻松地从另一个访问其中一个。只需在ControlB中创建一个您想要访问的公共属性,例如:

Public ReadOnly Property ControlB_DDL() As DropDownList
    Get
        Return Me.ddlItems
    End Get
End Property

然后你可以在ControlA中引用那个属性找到那个控件后:

ControlB ctrlB = (ControlB)Page.FindControl("cB");
DropDownList ddl = ctrlB.ControlB_DDL;

查看更多信息:http://www.dotnetcurry.com/ShowArticle.aspx?ID=155

访问。aspx中。ascx的控件。

HiddenField selectedEmailsId = performanceReportCtrl.FindControl("CONTROLID") as HiddenField;

和在ascx.

中访问aspx的控制
HiddenField selectedEmailsId = Page.FindControl("CONTROLID") as HiddenField;

最新更新