gpt4 book ai didi

asp.net - 如何在 EditItemTemplate 中使用 Bind() 来获取可为 null 的属性?

转载 作者:行者123 更新时间:2023-12-02 21:04:41 25 4
gpt4 key购买 nike

我有一个绑定(bind)到某个实体数据源的GridView。编辑已启用。 GridView 中显示的实体与另一个实体关联。假设我正在显示正在创建产品机器(每台机器没有或只有一个产品类型)。我在 gridview 中显示产品名称

<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# ((Machine)Container.DataItem).Product == null ? "-" : ((Machine)Container.DataItem).Product.Name %>'>
</asp:Label>
</ItemTemplate>

效果很好。现在我希望能够在 EditItemTemplate 中编辑关联产品。所以我添加了

<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddProducts" DataSourceID="dsProducts"
DataTextField="Name" DataValueField="ProductID"
SelectedValue='<%# Bind("Product.ProductID") %>'
AppendDataBoundItems="true">
<asp:ListItem Text="" Value="0"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>

它不起作用,因为当机器没有关联的产品时,就没有匹配的SelectedValue,并且无论如何Product都是null。我向 DropDownList 添加了空项目,当机器没有产品时应选择该项目。而且,如果我用产品编辑机器并选择此项,则列应为空。

我怎样才能实现这个目标?

最佳答案

我现在可以使用它了。 GridView 自动将 null 转换为空字符串,因此问题不在于绑定(bind),而在于我为 null 值添加的特殊项。

改变

<asp:ListItem Text="" Value="0"></asp:ListItem>

<asp:ListItem Text="" Value=""></asp:ListItem>

立即解决了问题...

关于asp.net - 如何在 EditItemTemplate 中使用 Bind() 来获取可为 null 的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7031054/

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