ASP.net Webforms and the Passive View Design Pattern
This is more a personal reminder than an article.
From the view we can get the typed user name and set a message.
Now, let's update our default.aspx to become our view. First we need to add a textbox, named txtUserName, a button, named btnGreetUser and a literal that will hold our greeting message (named litMessage).
Next, in the code-behind file of our default.aspx page, we add the IHomeView and implement its properties:
Notice that:
- The page_load event of the page initializes a Presenter passing the instance of the view;
- Our View properties map to both the textbox, for getting the name, and to the literal, for setting the message;
- The button click handler calls a method of the Presenter.
Now that we've build the View we can't yet run the code: The Presenter is missing.
As I stated before, the Presenter will handle all te responses to user events, in this case, the button click event. So based on the needs of the view, our presenter will look like so:
Taking a closer look at the OnGreetUser() we can see that we take the user name from the view, build a simple greeting message and set back the message to the view thru the Message property of the view.
"That's all folks..."
We're done. If we run our application we can see how everything is hook up and a nice message is shown to the user when we types his name and presses the button.
Since there's a separation of the UI logic from the view implementation we can easly test our code:
Leave a comment...