【问题标题】:ASP.NET Reference dynamically created control back in server aspx.cs server codeASP.NET 参考在服务器 aspx.cs 服务器代码中动态创建控件
【发布时间】:2015-03-14 05:09:57
【问题描述】:

服务器创建的控件带来的无尽问题仍在继续。

我有不同组的用户,每个组都有不同的用户信息。我正在使用 Telerik RadGrid 创建一个页面来管理组中的用户。由于网格列的未知性质,我完全在服务器上创建网格。您不能在 aspx 页面中定义网格并在服务器 aspx.cs 代码中添加列,各种事情都会中断,例如排序、过滤和获取额外文本

我需要的一个功能是输出带有网格数据的 excel 文件。问题,我如何在服务器回调中引用网格。如果您查看Telerik Grid Export to Excel,则有一个按钮回调可以更改服务器上的网格值并在 ImageButton_Click 中的网格控件上启动 excel 导出。在我的例子中,RadGrid1 在服务器中的 Page_Init 中创建并添加到 asp:PlaceHolder。网格工作正常。

有没有办法在服务器 aspx.cs 代码中引用服务器添加的控件。放上控件id不会编译。

谢谢, 乔治

【问题讨论】:

    标签: c# asp.net reference


    【解决方案1】:

    我了解到添加的控件相当于带有 runtat="server" 的页面控件。要获取控件,您可以在具有 runat 的页面上的已知容器对象上使用查找控件。

           protected void Page_Init(object source, System.EventArgs e)
           {
                 RadGrid adminGrid = new RadGrid();
                 adminGrid.NeedDataSource += new GridNeedDataSourceEventHandler(AdminGrid_NeedDataSource);
                 adminGrid.ID = "AdminGrid";
    
                 // lots of code building adminGrid 
    
                 this.GridPlaceHolder.Controls.Add(adminGrid);
           }
    
            protected void AdminGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
            {
               // AdminGrid would be the variable name if the control was
               // in the page with a runat instead of added programmatically
               // with an id of AdminGrid
               RadGrid grid = this.GridPlaceHolder.FindControl("AdminGrid") as RadGrid;
               grid.DataSource = DataSource;
            }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 2012-07-12
      • 2011-02-23
      • 2014-08-26
      • 1970-01-01
      • 2011-03-02
      • 2013-10-03
      相关资源
      最近更新 更多