Solved

FindControl for Web User Control

Posted on 2004-08-24
5
1,389 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where can I ask about my ASP.NET MVC project? 6 36
IIS redirect 1 68
Moving code from one TFS branch to another 3 25
Expression Evaluater 3 26
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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