How to use ButtonFields in Dynamic GridViews
-
24-10-2019 - |
Question
I'm having trouble concerning using dynamic web controls inside a dynamically created Gridview. I need to create the Gridview in the code behind because I'm trying to create a plugin architecture for controls to be added to a web page. Here is some example code to replicate the problem.
ResultControl : WebControl in dll
protected override void OnInit(EventArgs e)
{
GridView gv = new GridView();
ButtonField f = new ButtonField();
f.Text = "test";
f.CommandName = "test";
gv.Columnds.Add(f);
Controls.Add(gv);
}
Then in code behind of a web form.
protected override void OnInit(EventArgs e)
{
ResultControl rc = new ResultControl();
Controls.Add(rc);
}
this error on page Control 'POPlugin_ResultControl_ctl00' of type 'GridView' must be placed inside a form tag with runat=server.
I've been searching for answers but the only things I've found are about exporting the GridView to excel files and I wasn't sure if that was entirely related. Perhaps my google fu is weak but I would much appreciate anyone shedding some light on this situation.
Solution
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield.aspx. "The ButtonField class is used by data-bound controls (such as GridView and DetailsView) to display a button for each record that is displayed." But i don't see any datasource in your code.