English 中文(简体)
使用FindControl检索ListView
原标题:Retrieving ListView using FindControl
  • 时间:2011-02-13 15:30:55
  •  标签:
  • c#
  • asp.net

我试图从ascx控件中检索两个列表视图,以便将它们保存到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()方法。

来自MSDN for FindControl()

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

显然,如果您试图定位的列表视图不在Template中,那么您应该能够在代码隐藏中直接访问它们。但如果它在一个模板中,比如GridView的行,那么你可以这样访问它。

ListView listView1 = (ListView) GridView1.Rows[0].FindControl("ListView1");
问题回答

您的代码应更改为:

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签