c# - 使用 FindControl 检索 ListView

标签 c# asp.net

我正在尝试从 ascx 控件中检索两个 ListView ,以便将它们保存到 PDF 文件中:

<TagCloud:TagCloudControl ID="TagCloudControl1" runat="server" />

我收到以下错误:TagCloudControl1 是一个字段,但像类型一样使用,非静态字段、方法或属性需要对象引用...感谢您的帮助!

ListView lv1 = (TagCloudControl1)ListView.FindControl("ListView1");
ListView lv2 = (TagCloudControl1)ListView.FindControl("ListView2");

lv1.RenderControl(htWriter);
lv2.RenderControl(htWriter);

最佳答案

我从未见过或使用过静态 FindControl() 方法。

From MSDN for FindControl()

Searches the current naming container for a server control with the specified id parameter.

显然,如果您尝试查找的 ListView 不在模板中,您应该能够在代码隐藏中直接访问它们。但是如果它在模板中,例如 GridView 的行,那么您可以像这样访问它。

ListView listView1 = (ListView) GridView1.Rows[0].FindControl("ListView1");

关于c# - 使用 FindControl 检索 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4984999/

相关文章:

asp.net - ASP.NET 中缓存的模式和最佳实践是什么?

c# - 类型 'System.Web.HttpInputStream' 未标记为可序列化

c# - 如何知道System.Windows.Forms.dll中System.InvalidOperationException的细节?

c# - 从 Xaml 绑定(bind) RichTextBox 的文本

c# - 代码契约、继承和里氏原理

asp.net - 基于iPhone Phonegap的应用程序加载由Componentone组成的外部网站

c# - 在服务器上执行一个 EXE

c# - View 模型错误 : Object reference not set to an instance of an object

c# - 在jquery中调用代码隐藏函数

c# - 如何将字符串的值设置为下拉列表的所有值asp.net