首页 > 解决方案 > ListView InsertItemPosition not changing on Databound event

问题描述

I am using a ListView to show user info, stored in a table. I only want to show one record.

When the user first starts using the site, there will not be an initial record, so I need the InsertItemTemplate to be visible. Once a record is inserted, I need the InsertItemTemplate to disappear.

Based upon research, I have used the DataBound event to check for ListView.Item.Count and set the InsertItemPosition to either None or LastItem accordingly. I have also set a label on the page temporarily to display the results of the count.

The label is getting updated correctly. However, the InsertItemPosition is not changing until after a second postback occurs. So the result is the opposite of what it should be. It works correctly when the page loads, but then stays visible after I insert data, and then disappears if I delete the record. Any help would be appreciated. I have posted relevant code below.

protected void ListView1_DataBound(object sender, EventArgs e)
    {
        if (ListView1.Items.Count > 0)
        {
            lblRecordCount.Text = "DataBound - Records exist";
            ListView1.InsertItemPosition = InsertItemPosition.None;

        }
        else
        {
            lblRecordCount.Text = "DataBound - No records exist";
            ListView1.InsertItemPosition = InsertItemPosition.LastItem;


        }

    }

 Record Count: <asp:Label ID="lblRecordCount" runat="server" Text="Nothing Happened"></asp:Label>
        <br />

        <asp:ListView ID="ListView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSourceGift"  OnItemCreated ="ListView1_ItemCreated" OnDataBound="ListView1_DataBound"  >
            

标签: c#asp.netlistview

解决方案


添加您的ListView内部update panel,然后尝试显示/隐藏您的InsertItemTemplate.


推荐阅读