ASP.NET VB 将 Datalist 设置为编辑模式

ASP.NET VB Set Datalist to edit mode

我有一个 buttonDatalist 之上。每次用户进行搜索时,返回的记录将绑定到 Datalist.

按钮代码: <asp:Button ID="Button1" runat="server" Text="Edit" CommandName="edit"/>

数据列表代码:

<asp:DataList ID="UserMatrixDataList" runat="server" RepeatColumns="1" CellSpacing="6" RepeatLayout="Table" BorderWidth="5">
    <ItemTemplate>
        <table class="table">
            <col width="130">
            <col width="800">
            <tr>
                <td>
                    <strong>Level</strong>
                </td>
                <td>
                    <asp:Label ID="lblLevel" runat="server" Text='<%# Eval("Level")%>'></asp:Label>
                </td>
            </tr>
        </table>
    </ItemTemplate>
    <EditItemTemplate>
        <table class="table">
            col width="130">
                <col width="800">
            <tr>
                <td>
                    <strong>Level</strong>
                </td>
                <td>
                    <asp:TextBox ID="txtLevel" runat="server" Text='<%# Eval("Level")%>'></asp:TextBox>
                </td>
            </tr>
        </table>
    </EditItemTemplate>
</asp:DataList>

在 .vb 文件中,我有:

Protected Sub UserMatrixDataList_EditCommand(source As Object, e As DataListCommandEventArgs)

    For i = 0 To UserMatrixDataList.Items.Count

        UserMatrixDataList.EditItemIndex = e.Item.ItemIndex
        UserMatrixDataList.DataBind()

    Next
End Sub

但是,点击 button 后没有任何反应。知道我哪里做错了吗?或者甚至可以实现这样的方法?

由于按钮在Datalist之外,所以不会使用EditCommand方法。但是您仍然可以使用该按钮设置 EditIndex。

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'if you need this line depends on how and where you are binding the data to the datalist
    UserMatrixDataList.DataSource = source

    UserMatrixDataList.EditIndex = 6
    UserMatrixDataList.DataBind
End Sub

但是您一次只能将一项设置为edit