我有一個GridView
綁定到某個實體數據源。編輯已啓用。 GridView中顯示的實體與另一個實體關聯。假設我正在顯示Machines
,它們正在創建Products
(每臺機器無一個或一個產品類型)。我在gridview中顯示產品的名稱與如何在EditItemTemplate中使用Bind()爲空屬性?
<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,當機器沒有產品時應該選擇它。而且,如果我編輯帶有產品的機器並選擇此項目,則應該刪除列。
我該如何做到這一點?
請標記爲答案,如果這解決了您的問題。您可以標記爲您自己的答案。 – naveen
我還不能做到這一點。24小時或48小時需要通過,我不記得確切。 – Episodex
好。 – naveen