0

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. i.e 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

gsharp
  • 27,557
  • 22
  • 88
  • 134
Bruie
  • 1,195
  • 3
  • 11
  • 18
  • Also see http://stackoverflow.com/questions/2246777/raise-an-event-whenever-a-propertys-value-changed – nawfal May 05 '13 at 18:18

3 Answers3

0

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.

Chris B. Behrens
  • 6,255
  • 8
  • 45
  • 71
  • Its the knowing when a property has changed that I need help with. think its something like a listener event (observerable pattern) that i need help with. – Bruie Jan 27 '11 at 16:37
  • Ah...check out the answer to this very similar question: http://stackoverflow.com/questions/4738533/dirty-checking-for-an-object/4738604#4738604 – Chris B. Behrens Jan 27 '11 at 16:41
0

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.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • Not sure if this this what I want. I want something like a listener helper which from the Homepage will listen to see if it changes. I know this is possible just dont understand it enough!. – Bruie Jan 27 '11 at 16:36
  • But what are you going to do? After handling the event, do you want to change something in the page without a postback, or what? – Matías Fidemraizer Jan 28 '11 at 07:22
  • My issue is that I am dynamically creating a table with questions and possible answers(answers in a drop down). I have dynamically added a post back to the property in a table which at the moment will get the result of the question and put it into a property within a class (myResultsClass). What i want to do is when the property is changed in MyResultsClass for a method on the homePage.aspx to fire as this will then rebuild the rest of the table depending on the answer given. - hope this makes sense – Bruie Jan 28 '11 at 10:48
  • I'll try to answer you during this weekend, I'm @ work and this company don't think stackoverflowing is a good practice :( – Matías Fidemraizer Jan 28 '11 at 11:30
0

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

Community
  • 1
  • 1
neontapir
  • 4,698
  • 3
  • 37
  • 52