ASP.NET BindingContainer Property

The BindingContainer property gets the container control that contains the current control.

Syntax

public System.Web.UI.Control BindingContainer { get; }

Property Value

Returns a Control object representing the container of the current control.

Example

This example demonstrates how to use the BindingContainer property to access the parent control.

<%@ Page Language="C#" %>
<html>
<body>
  <form runat="server">

    <asp:Label ID="lbl" runat="server" Text="This is a label." />

    <asp:Button ID="btn" runat="server" Text="Click Me" OnClick="btn_Click" />

  </form>
</body>
</html>

<script runat="server">
  protected void btn_Click(object sender, EventArgs e)
  {
    // Get the parent container of the button
    Control parent = btn.BindingContainer;

    // Display the parent's ID
    Response.Write("Parent Control ID: " + parent.ID);
  }
</script>

Output:

Parent Control ID: form1

In this example, when the button is clicked, the BindingContainer property retrieves the parent control (the form), and its ID is displayed on the page.

Remarks

  • The BindingContainer property is useful for determining the hierarchical relationship between controls in a web form.
  • It helps in dynamically accessing or modifying parent controls during runtime.
  • Useful in scenarios involving nested controls or dynamic control creation.
© 2025 Rookie Tutorial. All rights reserved.