通过以编程方式设置其DataSource属性并调用其DataBind方法,使用ListView的EditItemTemplate修改数据

时间:2011-02-22 06:35:54

标签: c# asp.net vb.net listview

  

C#或VB.NET对我没问题。

我没有选择使用ListView的数据源控件。所有ListView都与代码隐藏的数据绑定。所以,我必须处理编辑/更新 ListView手动模式。

这是标记:

<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>

这是背后的代码:

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

当我点击ItemTemplate中的“编辑”链接以查看编辑模式时,我总是收到错误:

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

如何在以编程方式绑定数据的ListView中启用编辑模式?

谢谢。

2 个答案:

答案 0 :(得分:2)

Aaron有answer

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

答案 1 :(得分:0)

在lvList_ItemCommand中,您必须根据要传递的itemCommand添加要执行的代码。

您是否在此处添加了代码?

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

    'handling update logic

End Sub

如果你已经添加了代码,那么它们可能会错误...

请检查