gpt4 book ai didi

ASP.NET/DataList 的 DataItem 回发后为空

转载 作者:行者123 更新时间:2023-12-02 01:33:45 24 4
gpt4 key购买 nike

在我的 ASP.NET 表单中回发(单击按钮)后,表单的所有 DataItem 均为空。为什么?即使在回发后,我应该怎么做才能检索 DataList 的内容?

protected void buttonAddRecord_Click(object sender, EventArgs e)
{
foreach (DataListItem item in listFields.Items)
{
// item.DataItem == null WTF?
}
}

protected void Page_Load(object sender, EventArgs e)
{
BindFields();
}

private void BindFields()
{
object setting = MySettings.GetSetting();

if (!Null.IsNull(setting))
{
listFields.DataSource =
DataProvider.GetData(int.Parse(setting.ToString()));
listFields.DataBind();
}

listFields.Visible = listFields.Items.Count > 0;
emptyMessage.Visible = listFields.Items.Count == 0;
}

最佳答案

找到我的答案 here .

What John said, the data source items are only avaliable when databound. They are no longer accessable after initial loading.

You might consider having an object or object collection representing onscreen data that you update with the grid, then persist changes from that to databases.

更准确地说,我使用 HiddenField 跨帖子存储 ID,并从数据库请求数据,而不是尝试从 DataItem 获取数据(不能在数据绑定(bind)事件之外使用)。

The HiddenField control is used to store a value that needs to be persisted across posts to the server.

关于ASP.NET/DataList 的 DataItem 回发后为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3531531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com