ListView不会显示InsertItemTemplate

时间:2012-10-05 17:01:15

标签: c# asp.net listview

我在ListView内有AccordionPane,并带有“添加记录”按钮。单击“添加记录”按钮时,InsertItemTemplate不会出现。奇怪的是,如果我再次点击该按钮,它会出现。我尝试了很多操作,例如在按钮中添加CommandName="InitInsert"CommandName="Insert",将ListView从其中的Accordion中取出,取出“插入”按钮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;
    }

1 个答案:

答案 0 :(得分:1)

我相信你必须在更改位置时重新绑定ListView,因为模板构建过程是由绑定驱动的。不是100%肯定......