Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Avoid datasets if you can. Use strongly typed objects instead. If you do use datasets, they already serialize nicely. Here is a crude example to get you on your way. This will pull values from the text boxes and bind a list view to the collection. I hope this helps.</p> <p>Step 1: Create the Objects &amp; View Model</p> <pre><code> [Serializable] public class Person { public String FirstName { get; set; } public String LastName { get; set; } public Person(String FirstName, String LastName) { this.FirstName = FirstName; this.LastName = LastName; } public Person() : this(String.Empty, String.Empty) { } } [Serializable] public class ViewModelBase { } [Serializable] public class SomePageViewModel : ViewModelBase { private Person currentperson; public Person CurrentPerson { get { if (currentperson == null) { currentperson = new Person(); } return currentperson; } set { currentperson = value; } } private List&lt;Person&gt; persons; public List&lt;Person&gt; Persons { get { if (persons == null) { persons = new List&lt;Person&gt;(); } return persons; } set { persons = value; } } public SomePageViewModel() { } public void RegisterPerson(String FirstName, String LastName) { Persons.Add(new Person(FirstName,LastName)); } public void GetPersons() { /*Get Persons from database*/ } } </code></pre> <p>Step 2: Instantiate the ViewModel in your code behind file (PageName.aspx.cs)</p> <pre><code> /// &lt;summary&gt; /// Lazy initialized and persisted in session /// &lt;/summary&gt; public SomePageViewModel ViewModel { get { if (Session["SomePageViewModel"] == null) { Session.Add("SomePageViewModel", new SomePageViewModel()); } return Session["SomePageViewModel"] as SomePageViewModel; } set { if (value == null) { if (Session["SomePageViewModel"] != null) { Session.Remove("SomePageViewModel"); } } else { Session["SomePageViewModel"] = value; } } } </code></pre> <p>Step 3: Add markup to the ASPX</p> <pre><code> &lt;asp:TextBox runat="server" ID="txtFirstName" ValidationGroup="vgrpRegister" /&gt; &lt;asp:TextBox runat="server" ID="txtLastName" ValidationGroup="vgrpRegister" /&gt; &lt;asp:Button runat="server" ID="btnRegister" Text="Register" ValidationGroup="vgrpRegister"/&gt; &lt;asp:ListView runat="server" ID="lvPersons" ItemPlaceholderID="phPersons" DataSource='&lt;%#ViewModel.Persons%&gt;'&gt; &lt;LayoutTemplate&gt; &lt;table id="tblPersons" border="1"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;First Name&lt;/th&gt; &lt;th&gt;Last Name&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;asp:PlaceHolder runat="server" ID="phPersons"&gt;&lt;/asp:PlaceHolder&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/LayoutTemplate&gt; &lt;ItemTemplate&gt; &lt;tr&gt; &lt;td&gt;&lt;%#Eval("FirstName") %&gt;&lt;/td&gt; &lt;td&gt;&lt;%#Eval("LastName") %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/ItemTemplate&gt; &lt;/asp:ListView&gt; </code></pre> <p>Step 4: CodeBehind Events</p> <pre><code> protected void Page_Load(object sender, EventArgs e) { this.btnRegister.Click += new EventHandler(btnRegister_Click); } void btnRegister_Click(object sender, EventArgs e) { this.ViewModel.RegisterPerson(txtFirstName.Text, txtLastName.Text); this.lvPersons.DataBind(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload