Frage

Is there any similar strategy as dependency property in WPF to use in ASP.NET?

I want to create an UserControl in ASP.NET with a custom property, named for example "Car" then in the view I want to do something like this:

<CarControl Car="Ferrari" runat="server"/>
War es hilfreich?

Lösung

From your sample, I suspect that the main point is the ability to set the property in markup. This is possible with the following approach, though it does not have too much in common with a Dependency Property in WPF.

In your UserControl class in the CodeBehind file (named UserControlName.ascx.vb), you can add a property Car that you can set in the markup. You can open this file in the context menu by choosing View Code.

C#

public partial class CarControl : UserControl
{
    // ...
    public string Car { get; set; }
    // ...
}

VB.NET

Public Partial Class CarControl 
    Inherits UserControl

    ' ...

    Public Property Car As String

    ' ...

End Class

Markup

<My:CarControl ID="MyId" runat="server" Car="Ferrari" />
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top