Solved

FindControl for Web User Control

Posted on 2004-08-24
5
1,387 Views
Last Modified: 2008-02-01
I seem to have a problem. (as most of us who ask a question)

I am adding a web user control in a sharepoint webpart(this is just like a normal asp.net page so don't worry about that :)). I add this control in de Render method using:
myControl.Rendercontrol(output).
Before I add this control (or after, neither seem to work), I add an ID to it using:
MyControl.ID = "Test"

When the page refreshes I check to see if the control already exists (in the init method) using:
ObjMyControl = CType(Me.Page.FindControl("Test"), MyControl)
if ObjMyControl  is nothing then
 ObjMyControl  = new MyControl
end if

It never finds this control ? Any ideas why this doesn't work.

(the problem is that I set some properties of that control and  if I cant find that control It will make a new instance and I loose all of my set properties)
0
Comment
Question by:DoomsDirk
5 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 11882991
Hi,

have you tried to add the control in the Init event on the page so you will have the control after the first adding
otherwise you will lose control from the viewstate and you will not find it with FindControl method

Regards,
B..M
0
 
LVL 8

Accepted Solution

by:
shovavnik earned 500 total points
ID: 11883635
Are you sure you add the control to the page's Controls collection.

You *must* have

Page.Controls.Add( MyControl )

somewhere in your code behind.

Simply rendering a control that wasn't added to the collection only outputs it to the Response stream.  Actually, you probably don't need to override the Page.Render() method, as it automatically calls the Render method of each of it's child controls.

0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11884518
Try putting it in the Page_Load Event

Regards,

Aeros
0
 

Author Comment

by:DoomsDirk
ID: 11889789
When I try to use Page.controls.add(Mycontrol) I get an error saying:
The Controls collection cannot be modified because the control contains code blocks

Any ideas how to get around this ?
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 11889825
i think that you should first use
out = LoadControl("your ascx file")
Page.Controls.Add ( out )

B..M
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now