1

I have an ASP.NET Gridview control that allows you to select an employee.

The employee entity has a navigation property for group entity.

I have an ASP.NET ListView control that displays all the groups that an employee is in.

The SelectedIndexChanged function of the GridView looks like this

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DAL.SafetyContext sc = new DAL.SafetyContext();
        long empid = (long)GridView1.SelectedDataKey.Value;
        DAL.Employee emp = sc.Employees.Where(x => x.EID == empid).FirstOrDefault();
        ListView1.DataSource = emp.Groups;
        ListView1.DataBind();
    }

The problem is that I can't <%#Eval("Name") %> in the item template of the ListView because there is no data bound to control when the page loads.

Is there a way around this?

4

1 回答 1

1

尝试您的ListView. 这样,当没有数据时,您可以显示不尝试对(不存在的)“名称”数据进行 Eval 的内容。

<EmptyItemTemplate>
   <td runat="server">Nothing to see here</td>
</EmptyItemTemplate>

您可以让它符合您的 ItemTemplate 的结构,但只需在您进行数据绑定的所有位置保留空白/空字符串。

于 2013-05-15T19:34:34.513 回答