Narazana
Narazana

Reputation: 1950

Modifying data with the ListView's EditItemTemplate by programmatically settings its DataSource property and calling its DataBind method

C# or VB.NET is fine with me.

I don't have option to use data source control for ListView. All ListViews are bound with data from code-behind. So, I have to handle the Edit/Update mode of ListView manually.

Here's the mark-up:

<asp:ListView ID="lvList" runat="server" DataKeyNames="Id">
        <LayoutTemplate>
            <table id="TimeSheet" cellspacing="1" class="tablesorter">
                <thead>

                            <tr>
                                <th>
                                   Edit
                                </th>

                                <th>
                                    <a href="#">Ref. #</a>
                                </th>

                                <th>
                                    <a href="#">Category</a>
                                </th>


                            </tr>
                </thead>
                <tbody>
                    <tr id="itemPlaceholder" runat="server" />
                </tbody>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>

                <td>
                    <asp:LinkButton ID="lnkEdit" runat="server" ToolTip="Edit Category" CommandName="Edit">Edit</asp:LinkButton>
                </td>
                <td>
                <%#Eval("Id")%>
                </td>

                <td>
                    <%#HttpUtility.HtmlEncode(Eval("CategoryNameEN"))%>
                </td>


            </tr>
        </ItemTemplate>

<EditItemTemplate> 
      <p> 
         <b>Product Name:</b> 
         <asp:TextBox ID="txtCategoryEN" runat="server" Text='<%# Bind("CategoryNameEN") %>'></asp:TextBox> 
         <asp:RequiredFieldValidator ID="rfvCategoryName" ControlToValidate="txtCategoryEN" Display="Dynamic" 
                            runat="server" ErrorMessage="[Required]"></asp:RequiredFieldValidator> 
         <br /> 

      <p> 

          <asp:LinkButton ID="lnkUpdate" runat="server" ToolTip="Update Category" AlternateText="Update Category" CommandName="Update">Update</asp:LinkButton>         
          <asp:LinkButton ID="lnkCancel" runat="server" ToolTip="Cancel" AlternateText="Cancel" CausesValidation="false" CommandName="Cancel" >Cancel</asp:LinkButton>

      </p> 
   </EditItemTemplate> 

    </asp:ListView>

Here's the Code behind:

Protected Sub LoadListView()


    'GetGategory returns DataTable
    lvList.DataSource = GetCategory()
    lvList.DataBind()

End Sub

Protected Sub lvList_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles lvList.ItemCommand

    'handling update logic

End Sub

When I clicked on the "Edit" link in the ItemTemplate to see Edit mode I always get error:

    > System.InvalidOperationException: The ListView 'lvList' raised event ItemEditing which wasn't handled.

How do I enable the edit mode in ListView bound with data programatically?

Thank you.

Upvotes: 1

Views: 8651

Answers (2)

Tom
Tom

Reputation: 3374

Aaron has the answer

Protected Sub lvList_ItemEditing(sender as Object, e As ListViewEditEventArgs)
    lvList.EditIndex = e.NewEditIndex
    lvList.DataSource = SomeData
    lvList.DataBind()
End Sub

Upvotes: 2

Bhavik Goyal
Bhavik Goyal

Reputation: 2796

In lvList_ItemCommand you have to add the code to perform based on the itemCommand you are passing.

Have you have added code over here?

Protected Sub lvList_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles lvList.ItemCommand

    'handling update logic

End Sub

If you have added the code then their might be error over ther...

Please check

Upvotes: 0

Related Questions