Two-way databinding in ListView

2 posts, 0 answers
  1. Steinar
    Steinar avatar
    4 posts
    Member since:
    Aug 2013

    Posted 16 Aug 2013 Link to this post

    Is there a way to persist changes in a textbox within a listview to the correct object in a custom object list?
    I cannot find an elegant way to do this.

    ASPX:
    <telerik:RadWindow ID="rwCreateTask" runat="server" Modal="True" Height="600px" Width="400px">
            <ContentTemplate>
                <telerik:RadListView ID="RadListView1" runat="server">
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>
                                     
                                </td>
                                <td>
                                    Oppgave:
                                </td>
                                <td>
                                    Type:
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Line") == DBNull.Value ? "" : Eval("Line") %>'></asp:Label>
                                </td>
                                <td>
                                    <telerik:RadTextBox ID="RadTextBox1" runat="server" Text='<%# Bind("Text") == DBNull.Value ? "" : Bind("Text") %>' OnTextChanged="RadTextBox1_TextChanged"></telerik:RadTextBox>
                                </td>
                                <td>
                                    <telerik:RadDropDownList ID="RadDropDownList1" runat="server" DataSourceID="taskTypeSource" DataTextField="Name" DataValueField="TaskTypeId">
                                    </telerik:RadDropDownList>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </telerik:RadListView>
                <telerik:RadButton ID="RadButton1" runat="server" Text="Legg til oppgave" OnClick="RadButton1_Click"></telerik:RadButton>
            </ContentTemplate>
        </telerik:RadWindow>

    Code-behind:
    public partial class TaskListControl : System.Web.UI.UserControl
        {
            BindingList<CogTaskGenItem> taskItems = new BindingList<CogTaskGenItem>();
            CogTaskGenItem taskItem = new CogTaskGenItem();
     
             protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["taskItems"] != null)
                {
                    taskItems = (BindingList<CogTaskGenItem>)Session["taskItems"];
                }
            }
     
            protected void btnCreateTask_Click(object sender, EventArgs e)
            {
                string script = "<script>openWindow();</script>";
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "OpenTaskWindow", script);
            }
      
            public void RadButton1_Click(object sender, EventArgs e)
            {
                AddTaskItem();
            }
      
            private void AddTaskItem()
            {
                taskItem = new CogTaskGenItem();
                taskItem.Line = taskItems.Count + 1;
                taskItems.Add(taskItem);
                RadListView1.DataSource = taskItems;
      
                Session["taskItems"] = taskItems;
            }
           
            protected void RadTextBox1_TextChanged(object sender, EventArgs e)
            {
                taskItems.Clear();
      
                foreach (var item in RadListView1.Items)
                {
                    taskItem = (CogTaskGenItem)item.DataItem;
                    taskItem.Text = ((RadTextBox)sender).Text;
                    taskItems.Add(taskItem);
                }
      
                RadListView1.DataSource = taskItems;
                Session["taskItems"] = taskItems;
            }
        }

  2. Marin
    Admin
    Marin avatar
    1043 posts

    Posted 21 Aug 2013 Link to this post

    Hello,

     Normally the textbox control persists its text changes on postback.
    The recommended way of saving data from the ListView can be found in the following demos:
    http://demos.telerik.com/aspnet-ajax/listview/examples/dataediting/net35automaticdataediting/defaultcs.aspx
    You put an item in edit mode, then edit the data and click the save button to persist the data.
    Otherwise if you need to persist the changed text on any kind of postback, your approach with the TextChanged event of the textbox seems reasonable enough.

    Regards,
    Marin
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top