Find aspx control with ascx

I am trying to find a label on an aspx page from a user control (ascx) on the specified aspx page. Obviously, Page.FindControl ("lablel1") is not working. Do I need to add somewhere in ClientID? Thanks.

+3
source share
10 answers

I think you should stop and think about your design. Your controls should never know anything about the page that contains them - the fact that you need to find the control on the page from another control tells me that you have to rethink the problem.

The best I can tell you (with the fact that I know little about your architecture) is that you should pass a link to the control that you hope to find in your user control. Thus, your control does not need to know about things outside of you.

+14
source

When using the FindControl()immediate parent of a control out of context, you need to go through the control tree to find what level yours is labelat and call it .FindControl()at the appropriate level.

However, take @Andrew Hare's advice and rethink your architectural decisions. There is probably a better way for your UserControl to interact with its consumer page.

, UserControl ( / ). , , , .

, Error .

:

Public Event UserErrorOccured(ByVal ErrorText as String)

:

If Not Page.IsValid Then
    RaiseEvent("The page is not valid")
End If

:

protected sub UserEventHandler(ByVal ErrorText as String) Handles MyUserControl.UserErrorOccured
    errorLabel.Text = ErrorText
End Sub
+3

- , .

Me.Owner.FindControl("controlName")

...

Me.Owner.Parent.FindControl("controlName")

...

Me.Owner.Parent.Parent.FindControl("controlName")

, () , . , , .

VB ( ) :

Protected Function FindControlByID(ByRef childControl As Control, ByVal ID As String) As Control
    Dim ctrl As Control = childControl.FindControl(ID)
    If Not ctrl Is Nothing Then
      Return ctrl
    Else
      If Not childControl.Parent Is Nothing Then
        Return FindControlByID(childControl.Parent, ID)
      Else
        Return Nothing
      End If
    End If
  End Function

:

Dim lbl As Label = FindControlByID(Me.Owner, "label1")
+2

, :

public interface IStatusDisplayer
{
   Label StatusLabel { get; }
}

, /. , :

var statusDisplayer = this.Page as IStatusDisplayer;
if (statusDisplayer != null)
{
    statusDisplayer.StatusLabel.Text = "Hello World!";
}
+2

Me.NamingContainer.FindControl("label1")
+1
Control ct = WebUserControl11.FindControl("DropDownList1");

DropDownList dt = (DropDownList)ct;

TextBox1.Text = dt.SelectedValue.ToString();
+1

. "", Session . - . AJAX , , , , , .

TheSteve , , . .

0

, ContentPlaceHolder:

Dim ContentPlaceHolder1 As ContentPlaceHolder = TryCast(Page.Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)

Then using ContentPlaceHolder you can find the identifier of the control on the aspx page:

TryCast(ContentPlaceHolder1.FindControl("LiteralOnParentASPXPage"), Literal).Text = "some text" 
0
source

This is a brute force method, but it works when the control is deeply immersed in the hierarchy of controls:

private Control GetTextEditor(ControlCollection controls)
{
    foreach (Control ctrl in controls)
    {
        if (ctrl.ID != null && ctrl.ID == "teMessage")
            return ctrl;
        if (ctrl.Controls.Count > 0)
        {
            Control inner = GetTextEditor(ctrl.Controls);
            if (inner != null)
                return inner;
        }
    }
    return null;
}
0
source

To access the on / off controls from .ascx in the .aspx file, try this code, it is also a solution.

protected void Page_Load(object sender, EventArgs e)
{
    Control ct = PEM.FindControl("btnInsert");
    Button btn = (Button)ct;
    btn.Enabled = false;
}
0
source

Source: https://habr.com/ru/post/1710611/


All Articles