在DataBind()之后不显示DetailsView控件

时间:2012-02-14 20:39:39

标签: asp.net detailsview

我的asp.net Web应用程序中有以下详细信息视图。它应该显示的是一个带有编辑链接的字符串,当选择编辑链接时,字符串将变为一个下拉列表,其中填充了一些选项。当Enumerable中没有数据时,DetailsView没有显示(甚至不显示编辑链接)......

 <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 个答案:

答案 0 :(得分:1)

如果您想要在EmptyDataTemplate数据源为空时显示任何内容,则需要添加DetailsView

<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等。