简体   繁体   中英

ASP.NET Reference dynamically created control back in server aspx.cs server code

And my endless problems from server created controls continue.

I am have users that in different groups and each group has different user information. I am creating a page to manage users in a group using a Telerik RadGrid. Because of the unknown nature of the grid columns I am creating the grid on completely on the server. You cannot have a grid defined in the aspx page and add columns in the server aspx.cs code, all kinds of things break like sorting, filters and getting extra text

A feature I need is to output excel files with the grid's data. The problem, how do I reference the grid in server calls backs. If you look at Telerik Grid Export to Excel there is a button callback that changes grid values on the server and initiates the excel export on the grid control in ImageButton_Click. In my case RadGrid1 is created in the server in the Page_Init and added to an asp:PlaceHolder. The grid works fine.

Is there any way to reference a server added control back in the server aspx.cs code. Putting the control id will not compile.

Thanks, George

I learned that added controls are equivalent to a page controls with runtat="server" . To get a control you can use the find control on a known container object on the page that has a 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; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM