2012-02-14 64 views
1

我在我的asp.net web應用程序中有以下detailsview。它應該顯示的是一個帶有編輯鏈接的字符串,當編輯鏈接被選中時,字符串變成一個下拉列表,其中填充了一些選項。當在可枚舉沒有數據DetailsView控件顯示不出來(甚至沒有編輯鏈接)...DetailsView控件在DataBind後不顯示()

<asp:DetailsView ID="dvAwardType" 
      runat="server" 
      AutoGenerateRows="False" 
      Width="100%" 
      SkinID="SampleDetailsView" 
      CssSelectorClass="PrettyDetailsView" 
      AutoGenerateEditButton="True" DataKeyNames="guidRequirementId" 
      OnModeChanging="dvAwardType_ModeChanging" 
      OnItemUpdating="dvAwardType_ItemUpdating"> 
      <FieldHeaderStyle Font-Bold="True" Width="150px" /> 
      <Fields> 
       <asp:TemplateField HeaderText="Pool Type"> 
        <ItemTemplate> 
         <%#Eval("strAwardType")%> 
        </ItemTemplate> 
        <EditItemTemplate> 
          <asp:DropDownList ID="ddlAwardType" 
           runat="server"> 
          <asp:ListItem Text="" Value="" /> 
          <asp:ListItem Text="Small Business" Value="SB" /> 
          <asp:ListItem Text="Large Business" Value="LB" /> 
         </asp:DropDownList> 
        </EditItemTemplate> 
       </asp:TemplateField> 
      </Fields> 
     </asp:DetailsView> 


protected void Page_Load(object sender, EventArgs e) 
{ 

    dvAwardType.DataSource = GetData(RequirementId); 
    dvAwardType.DataBind(); 
} 

private IEnumerable GetData(Guid guidRequirementId) 
{ 
    var data = Requirement.BusinessSize(guidRequirementId) 
     .Select(o => 
      new 
      { 
       guidRequirementId = o.guidRequirementId, 
       strAwardType = (o.strBusinessSize != null) ? TranslateSize(o.strBusinessSize) : "" 
      } 
      ); 
    return data; 
} 

private string TranslateSize(string strSize) 
{ 
    string strTranslated = string.Empty; 
    switch(strSize.Trim()) 
    { 
     case "SB": strTranslated = "Small Business"; 
      break; 
     case "LB": strTranslated = "Large Business"; 
      break; 
    } 
    return strTranslated; 
} 

回答

1

你需要的,如果你想要的東西在DetailsView顯示添加一個EmptyDataTemplate當它的數據源是空的:

<asp:DetailsView ID="dvAwardType" 
    runat="server" 
    AutoGenerateRows="False" 
    Width="100%" 
    SkinID="SampleDetailsView" 
    CssSelectorClass="PrettyDetailsView" 
    AutoGenerateEditButton="True" DataKeyNames="guidRequirementId" 
    OnModeChanging="dvAwardType_ModeChanging" 
    OnItemUpdating="dvAwardType_ItemUpdating"> 
    <EmptyDataTemplate> 
     There is no data to display 
    </EmptyDataTemplate> 

你可以把你喜歡的東西放在那個EmptyDataTemplate裏面。我只是包含文字,但您可以放置​​控件,超鏈接,HTML等。