Retrieving ListView using FindControl
I am trying to retrieve two listviews from an ascx control for the purpose of saving them to a PDF file:
<TagCloud:TagCloudControl ID="TagCloudControl1" runat="server" />
I get the following error: TagClou开发者_高级运维dControl1 is a field but is used like a type and an object reference is required for the non-static field, method or property...Thanks for your help!
ListView lv1 = (TagCloudControl1)ListView.FindControl("ListView1");
ListView lv2 = (TagCloudControl1)ListView.FindControl("ListView2");
lv1.RenderControl(htWriter);
lv2.RenderControl(htWriter);
I have never seen or used a static FindControl()
method.
From MSDN for FindControl()
Searches the current naming container for a server control with the specified id parameter.
Obviously if the listviews you are trying to locate are not in a Template, you should be able to access them directly in code-behind. But if it in a Template such as a GridView's Row then you can access it like this.
ListView listView1 = (ListView) GridView1.Rows[0].FindControl("ListView1");
Your code should be changed to this:
var lv1 = (TagCloudControl)ListView.FindControl("ListView1");
var lv2 = (TagCloudControl)ListView.FindControl("ListView2");
精彩评论