简体   繁体   中英

Watch when property changes

I am creating a web application!

I have a class which has a property on it :

public class DependencyHelper: INotifyPropertyChanged
{

    private string _myString;
    public virtual string MyString
    {
        get { return _myString; }
        set 
        {
            _myString = value;
            OnPropertyChanged("MyString");
        }
    }

I want to have something like an event on my home page homepage.aspx.cs which will do something once the property on the class has changed. ie will get the value of MyString and display it in an alert.

Can someone please help with this as I can not think of how to do it.

Thank you

Since it's a web page, you have to emit JS out to the client to display the message box. I would do something like this:

private void OnPropertyChanged(string propertyName)
{
    ScriptManager.RegisterClientScript(typeof(Page), "AlertScript" + System.Guid.NewGuid().ToString(), string.Format("alert('Property {0} has changed');"));
}

The whole NewGuid mess makes it so that the script name is unique, and if you change more than one property, you'll get more than one box.

I don't know if you want two-way binding in a Web solution, which is absoutely impossible, or possible using comet approach (http://en.wikipedia.org/wiki/Comet_(programming)).

Perhaps you're looking to do such thing after a postback or callback, and this is possible just adding an event hanlder to the PropertyChanged event from your control or page, and add some script block or reference one to show an alert with the changed value.

Check how to create an ScriptControl http://byatool.com/ui/creating-script-controls-and-love/

And ASP.NET Callback API: http://msdn.microsoft.com/en-us/library/ms178208.aspx

You can do a comet approaching or just a one-way binding so, in the second case (the one suggested and recommended), after an user interaction, use a callback to the server, check if something changed and then, back in the client, alert this change.

This is similar to question 5216766 . I suggest you use an ObservableCollection or an AsyncBindingList .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM