I have a textbox in asp page which is a concrete view. User enters a string of 2000 characters long. When save button is pressed, this value is saved into database. After save the page I redirected to another page where the entered value is shown as a label. I used to store the value of the textbox in session. How do I handle it when I am using it in MVP?
1) How to set the value of session using MVP concepts in TextInputPage?
2) How to display the result in label after reading from session using MVP concepts?
Note: There is some processing (text appending) before adding value to session.
It would be great if you can answer with code example rather than pointing to another post. This is the moste simplest example (I think 🙂 ) for new comers to learn.
using System;
namespace ViewInterfaces
{
public interface ITextView
{
string InputtedText { get; }
event EventHandler ButtonClickedEvent;
}
}
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
<asp:Button ID="btnGoToResult" runat="server" Text="Go To Result"
onclick="btnGoToResult_Click" />
</div>
</form>
using System;
using ViewInterfaces;
using Presenter;
using Model;
public partial class TextInputPage : System.Web.UI.Page, ITextView
{
public event EventHandler ButtonClickedEvent;
private TextPresenter listPresenter;
protected void Page_Load(object sender, EventArgs e)
{
TextModel modelController = new TextModel();
listPresenter = new TextPresenter(this, modelController);
this.listPresenter.OnViewLoaded();
}
public string InputtedText
{
get
{
return txtInput.Text;
}
}
protected void btnGoToResult_Click(object sender, EventArgs e)
{
if (ButtonClickedEvent != null)
{
//Riase the event
ButtonClickedEvent(this, e);
}
//Session does not use any MVP now.
if (txtInput.Text.Length > 0 && txtInput.Text.Length <= 100)
{
Session["TextInput"] = "1-100 "+txtInput.Text;
}
else if (txtInput.Text.Length > 100 && txtInput.Text.Length <= 1000)
{
Session["TextInput"] = "101-1000" + txtInput.Text;
}
else
{
Session["TextInput"] = "1001 - 2000" + txtInput.Text;
}
//Redircting currenlty does not use any MVP concept
Response.Redirect("/SessionTestWebSite/ResultOutputPage.aspx");
}
}
<form id="form1" runat="server">
<div>
<asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label>
</div>
</form>
using System;
public partial class ResultOutputPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Does not use MVP now
lblResult.Text = (string) Session["TextInput"];
}
}
using System;
using ViewInterfaces;
using Model;
namespace Presenter
{
public class TextPresenter
{
private ITextView viewObj;
private TextModel contactsModelController;
public TextPresenter(ITextView view, TextModel controller)
{
viewObj = view;
contactsModelController = controller;
}
//Presenter Method
public virtual void OnViewLoaded()
{
//Event subscription
viewObj.ButtonClickedEvent += new EventHandler(DetailView_EditClicked);
}
void DetailView_EditClicked(object sender, EventArgs e)
{
//Calling Model's Functionality
contactsModelController.StoreText(viewObj.InputtedText);
}
}
}
namespace Model
{
public class TextModel
{
public void StoreText(string inputString)
{
//Store to database
}
}
}
References:
mvp session response request
I usually use next way:
View implementation:
Presenter:
In this case used only setter of ITextView.SessionTextEntry so getter can be removed.
Hope it helps…