Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo-way data binding in ASP.NET
    primarykey
    data
    text
    <p>Trying to use data binding between a list of objects and a data list control. What I want to do are </p> <ol> <li>create the list of objects </li> <li>have them bound to the controls </li> <li>change data in the UI </li> <li>have the changes in the ui bound to the list of objects </li> <li>on post back - have the list of objects with the new values from the ui </li> </ol> <hr> <pre><code>&lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;asp:DataList ID="DataList1" runat="server" DataKeyField="ClassID" ViewStateMode="Enabled"&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID="txtValue1" runat="server" Text='&lt;%# Bind("Value1") %&gt;'&gt;&lt;/asp:TextBox&gt; &lt;asp:TextBox ID="txtValue2" runat="server" Text='&lt;%# Bind("Value2") %&gt;'&gt;&lt;/asp:TextBox&gt; &lt;asp:TextBox ID="txtvalue3" runat="server" Text='&lt;%# Bind("Value3") %&gt;'&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:DataList&gt; &lt;asp:Button ID="btnDoPostBack" runat="server" Text="Do Post Back" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; </code></pre> <hr> <pre><code>Option Explicit On Option Strict On Imports System.Diagnostics Partial Class _Default Inherits System.Web.UI.Page Dim Class1List As List(Of Class1) Protected Sub Page_PreLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreLoad Dim txtValue1 As TextBox Dim txtValue2 As TextBox Dim txtValue3 As TextBox Dim ItemIndex As Integer = 0 If Page.IsPostBack Then Class1List = CType(Session("Class1List"), List(Of Global.Class1)) 'Class1List = CType(DataList1.DataSource, List(Of Global.Class1)) For Each myDataListItem As DataListItem In DataList1.Items txtValue1 = CType(myDataListItem.FindControl("txtValue1"), TextBox) Long.TryParse(txtValue1.Text, Class1List(ItemIndex).Value1) txtValue2 = CType(myDataListItem.FindControl("txtValue2"), TextBox) Integer.TryParse(txtValue2.Text, Class1List(ItemIndex).Value2) txtValue3 = CType(myDataListItem.FindControl("txtValue3"), TextBox) Class1List(ItemIndex).Value3 = txtValue3.Text ItemIndex += 1 Next End If End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim myClass1 As Class1 If Not Page.IsPostBack Then Class1List = New List(Of Class1) myClass1 = New Class1 Class1List.Add(myClass1) BindData() Else 'Class1List = CType(DataList1.DataSource, List(Of Global.Class1)) Debug.WriteLine("Page_Load, Value1 = " &amp; Class1List(0).Value1.ToString()) Debug.WriteLine("Page_Load, Value2 = " &amp; Class1List(0).Value2.ToString()) Debug.WriteLine("Page_Load, Value3 = " &amp; Class1List(0).Value3) End If End Sub Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload Session("Class1List") = Class1List End Sub Sub BindData() DataList1.DataSource = Class1List DataList1.DataBind() End Sub Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound Dim myClass1 As Class1 If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then myClass1 = CType(e.Item.DataItem, Class1) Debug.WriteLine("DataList1_ItemDataBound, Value1 = " &amp; myClass1.Value1.ToString()) Debug.WriteLine("DataList1_ItemDataBound, Value2 = " &amp; myClass1.Value2.ToString()) Debug.WriteLine("DataList1_ItemDataBound, Value3 = " &amp; myClass1.Value3) End If End Sub Protected Sub btnDoPostBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDoPostBack.Click Dim myRandom As New Random Class1List(0).Value1 = myRandom.Next(100) Class1List(0).Value2 = myRandom.Next(100) Class1List(0).Value3 = myRandom.Next(100).ToString() Debug.WriteLine("btnDoPostBack_Click, Value1 = " &amp; Class1List(0).Value1.ToString()) Debug.WriteLine("btnDoPostBack_Click, Value2 = " &amp; Class1List(0).Value2.ToString()) Debug.WriteLine("btnDoPostBack_Click, Value3 = " &amp; Class1List(0).Value3) BindData() End Sub End Class </code></pre> <hr> <p>The Class Class1 is trivial:</p> <pre><code>Option Explicit On Option Strict On Imports Microsoft.VisualBasic Public Class Class1 Private _ClassID As Long Private _Value1 As Long Private _Value2 As Integer Private _value3 As String = String.Empty Public Property ClassID As Long Get Return _ClassID End Get Set(ByVal value As Long) _ClassID = value End Set End Property Public Property Value1 As Long Get Return _Value1 End Get Set(ByVal value As Long) _Value1 = value End Set End Property Public Property Value2 As Integer Get Return _Value2 End Get Set(ByVal value As Integer) _Value2 = value End Set End Property Public Property Value3 As String Get Return _value3 End Get Set(ByVal value As String) _value3 = value End Set End Property End Class </code></pre> <hr> <p>Update: I got the code behind above to do what I want it to do - I was thinking there was a better way?</p>
    singulars
    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.
 

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