如何在动态 GridView 中使用 ButtonField
我在动态创建的 Gridview 中使用动态 Web 控件时遇到问题。我需要在后面的代码中创建 Gridview,因为我正在尝试为要添加到网页的控件创建一个插件体系结构。这是一些复制问题的示例代码。
ResultControl : dll 中的 WebControl
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);
}
然后在 Web 表单的代码后面。
protected override void OnInit(EventArgs e)
{
ResultControl rc = new ResultControl();
Controls.Add(rc);
}
页面上出现此错误 “GridView”类型的控件“POPlugin_ResultControl_ctl00”必须放置在带有 runat=server 的表单标记内。
我一直在寻找答案,但我发现的唯一的事情是将 GridView 导出到 Excel 文件,我不确定这是否完全相关。也许我的谷歌福很弱,但我非常感谢任何人对这种情况有所了解。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://msdn.microsoft.com/en -us/library/system.web.ui.webcontrols.buttonfield.aspx。 “数据绑定控件(例如 GridView 和 DetailsView)使用 ButtonField 类来为所显示的每条记录显示一个按钮。”但我在您的代码中没有看到任何数据源。
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.