0

我有一个ListView带有AccordionPane“添加记录”按钮的内部。当我单击“添加记录”按钮时,InsertItemTemplate不会出现。奇怪的是,如果我再次单击该按钮,它确实会出现。我已经尝试了很多事情,比如添加CommandName="InitInsert"CommandName="Insert"到按钮,从ListViewAccordion里面取出,把“插入”按钮从里面ListView取出Accordion,都无济于事。我有什么明显的遗漏吗?我究竟做错了什么?

<asp:ListView runat="server" ID="List1"  
    OnItemDataBound="List1_ItemDataBound"
    OnItemCommand="List1_ItemCommand"
    OnItemEditing="List1_ItemEditing"
    OnItemUpdating="List1_ItemUpdating"
    OnItemCanceling="List1_ItemCancelling"
    OnItemDeleting="List1_ItemDeleting"
    OnItemInserting="List1_ItemInserting"
    OnSorting="List1_Sorting">
<LayoutTemplate>
    <table border="0" cellpadding="1" cellspacing="0">
    <thead>
    <tr>
        <th><asp:LinkButton runat="server" ID="BtnCmpnyId" CommandName="Sort" CommandArgument="CmpnyId" Text="Company Code" /></th>
        ... more columns ...
    </tr>
    </thead>

    <tbody>
        <tr runat="server" id="itemPlaceholder"></tr>
    </tbody>

    <tfoot>
        <tr><td colspan="7" style="text-align: center;">
            <asp:Button runat="server" ID="BtnAddRecord" Text="Add Record" OnClick="BtnAddRecord_Click" />
        </td></tr>
    </tfoot>
    </table>
</LayoutTemplate>

<InsertItemTemplate>
    <tr>
        <td>
            <asp:HiddenField runat="server" ID="HiddenID" Value="-1" />
            <asp:TextBox runat="server" ID="TextCmpnyId" Text='<%# Eval("CmpnyId") %>' MaxLength="6" Width="50" />
        </td>

        ... more fields ...
    </tr>
</InsertItemTemplate>

... other templates ...
</asp:ListView>

在我后面的代码中:

protected void BtnAddRecord_Click(object sender, EventArgs e)
{
    List1.EditIndex = -1;
    List1.InsertItemPosition = InsertItemPosition.LastItem;
    //Button button = (Button)List1.Controls[0].FindControl("BtnAddRecord");
    //button.Visible = false;
}

// ItemCommand() is an empty shell for now...
protected void List1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    switch (e.CommandName.ToLower())
    {
        case "sort":
            break;

        case "edit":
            break;

        case "insert":
            break;

        case "update":
            break;

        case "cancel":
            break;

        case "delete":
            break;
    }
4

1 回答 1

1

我相信您在更改位置时必须重新绑定 ListView,因为模板构建过程是脱离绑定的。不是100%确定...

于 2012-10-05T19:05:17.873 回答