我可以N#39;从UserControl的代码后面访问用户控件中的控件

时间:2014-07-16 12:06:52

标签: asp.net

我有一个Asp.Net UserControl。它有一个GridView。

但我无法在UserControl的CodeBehind中访问此GridView。

这是什么原因?

感谢您的回答。

OrdersControl.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="~/Controls/OrdersControl.ascx.cs" CodeBehind="OrdersControl.ascx.cs" Inherits="EntityApp.Controls.OrdersControl"%>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>

OrdersControl.ascx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        NorthwindEntities = new NorthwindEntities();

        GetOrders();
    }

    private void GetOrders()
    {
        var orders = (from order in NorthwindEntities.Orders
                      join customer in NorthwindEntities.Customers on order.CustomerID equals customer.CustomerID
                      join employee in NorthwindEntities.Employees on order.EmployeeID equals employee.EmployeeID
                      join shipper in NorthwindEntities.Shippers on order.ShipVia equals shipper.ShipperID
                      select new
                      {
                          order.OrderID,
                          order.OrderDate,
                          order.RequiredDate,
                          order.ShippedDate,
                          customer.CompanyName,
                          customer.ContactName,
                          customer.City,
                          customer.Phone,
                          employee.FirstName,
                          employee.LastName,
                          shipperCompany = shipper.CompanyName,
                          shipperPhone = shipper.Phone
                      }).ToList();

        GridView1.DataSource = orders;//Error is in here
        GridView1.DataBind();//Error is in here
    }
}

}

0 个答案:

没有答案