Extending GridView with default buttons and update panel
I am trying to extend the ASP GridView control by adding some buttons above it. Such as Insert/Delete/Print etc. I must extend the GridView control because it is used at a lot of places in the project. The buttons and the开发者_开发问答 control must be wrapped in an UpdatePanel so it can do partial updating. I am trying to do something like this:
UpdatePanel up = new UpdatePanel();
ImageButton newButton = new ImageButton();
newButton.ImageUrl = "../Images/new.gif";
up.ContentTemplateContainer.Controls.Add(newButton);
up.ContentTemplateContainer.Controls.Add(this);
Page.Form.Controls.Add(up);
The problem is I don't know where to put it, and I don't even know if this will work. If I put it at OnLoad/OnPreRender I get this:
"The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases."
This error occures on the second add.
If I put it in CreateChildControls Page is not yet created.
Please give me some pointers.
In this situation, I'd probably build my own custom server control that inherits from GridView.
Here's a link to a MSDN article that should get you started: Walkthrough: Developing and Using a Custom Web Server Control
EDIT:
Since you're already creating the custom server control; I think this might be your problem line: Page.Form.Controls.Add(up)
Try using simply Controls.Add(up)
. What it looks like you're trying to do is add the update panel to the Page itself; when you should really be adding it to the controls collection of your extended GridView
精彩评论