Bob Kaufman
Bob Kaufman

Reputation: 12815

ListView won't display InsertItemTemplate

I have a ListView inside of an AccordionPane with an "Add Record" button. When I click the "Add Record" button, the InsertItemTemplate does not appear. Strangely, if I click the button a second time, it does appear. I've tried a number of things like adding CommandName="InitInsert" or CommandName="Insert" to the button, taking the ListView out of the Accordion that it's inside of, taking the "Insert" button out of the ListView then out of the Accordion, all to no avail. Is there something obvious that I'm missing? What am I doing wrong?

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

In my code behind:

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

Upvotes: 0

Views: 3192

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

I believe you have to rebind the ListView when you change the position, because the template building process is driven off binding. Not 100% sure...

Upvotes: 1

Related Questions