c# - 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" >
解决方案
添加您的ListView
内部update panel
,然后尝试显示/隐藏您的InsertItemTemplate
.
推荐阅读
- r - 有没有办法编辑你已经刮掉的小标题的值?
- ruby - Ruby中如何修改Array类中map函数的实现
- javascript - 如何在javascript中以带2位小数的指数格式显示变量?
- javascript - 在签署 jwt 时,只保留 user_id 或整个用户对象是否更好?
- ruby-on-rails - 使用 Rails 6 Zeitwerk 时,具有相同名称的类会导致未初始化的常量
- css - 引导卡体内容溢出
- linux-kernel - 如何使用 overlayfs 动态加载设备树覆盖?
- python - 使用python在条形图中可视化数据
- azure-ad-b2c - 获取范围令牌时出错:{guid} ClientAuthError:需要用户登录。对于静默呼叫,请求必须包含 sid 或 login_hint
- node.js - 如何在koa流中将错误转发给客户端?