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

时间:2017-02-08 01:33:08

标签: asp.net vb.net datalist

我的button位于Datalist之上。每次用户进行搜索时,返回的记录都将绑定到Datalist

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

Datalist的代码:

<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后没有任何反应。知道我在哪里做错了吗?或者甚至可以实现这样的方法?

1 个答案:

答案 0 :(得分:0)

由于按钮位于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