Solved

FindControl for Web User Control

Posted on 2004-08-24
5
1,392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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 …

756 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