Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

FindControl for Web User Control

Posted on 2004-08-24
5
Medium Priority
?
1,399 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

916 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