Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

FindControl for Web User Control

Posted on 2004-08-24
5
Medium Priority
?
1,395 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 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

715 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