How can I find the components added to web form? (Urgent)

M

Martin Hill

Hi there,

How can I find the components added to web form from my custom web
control?

Suppose I added a Timer component (with name "timer1") onto the web
form, from my user web
control, how can I locate the timer component by name "timer1"? The
Page.Controls collection only contains web controls but no components.

TIA, this is a urgent issue need to be resolved in my project.


-Martin
 
R

Rutger Smit

Martin said:
Hi there,

How can I find the components added to web form from my custom web
control?

Suppose I added a Timer component (with name "timer1") onto the web
form, from my user web
control, how can I locate the timer component by name "timer1"? The
Page.Controls collection only contains web controls but no components.

TIA, this is a urgent issue need to be resolved in my project.


-Martin

You can not add a Timer object to a form.
The Timer object has nothing to do with UI (user interface), it's a
object for the backend only, it's being used to trigger events on a
interval.

What did you expect? A clock on your page?

--

//Rutger

DoDotNet@KICKTHIS_Gmail.com
www.RutgerSmit.com
 
C

Cor Ligthert

Martin,

See this sample I did make a while ago.

It is not real for production, I think it is to slow however in the Form1
page is the timer you need I thought,

I hope it helps you?

Cor

\\\Form 1 Needs a imagebox on the page
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Image1.Height = New Unit(32)
Me.Image1.Width = New Unit(200)
Image1.Visible = True
Dim scriptString As String = "<script language=JavaScript>" & _
"setclock(); function setclock(){document.images.Image1.src = " & _

"'http://localhost/WebClock/WebForm2.aspx';setTimeout('setclock()',1000)}</s
cript>"
Page.RegisterStartupScript("setclock", scriptString)
End Sub
///
\\\Form2 needs nothing
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Response.Cache.SetExpires(DateTime.Now.AddTicks(500))
Dim myForeBrush As Brush = Brushes.Black
Dim myFont As New Font("Times New Roman", 8, FontStyle.Regular)
Dim textHeight As Single
Dim bm As New Bitmap(120, 20)
Dim g As Graphics = Graphics.FromImage(bm)
g.Clear(Color.White)
Dim textSize As SizeF = g.MeasureString("now.tostring", myFont)
g.DrawString(Now.ToString, myFont, myForeBrush, _
New RectangleF(0, 0, 120, 20))
Dim ms As New IO.MemoryStream
Dim arrImage() As Byte
bm.Save(ms, Imaging.ImageFormat.Bmp)
arrImage = ms.GetBuffer
Response.BinaryWrite(arrImage)
g.dispose
End Sub
///

"Martin Hill"
 
S

Steven Cheng[MSFT]

Hi Martin,

From your description, you put a .net Timer component on the ASP.NET web
page and want to access this timer from a certain control on that page,
yes? As for this problem, here are some of my suggestions:

1. The timer is not a UI control so it is not added into control collection
and just be a member of the page class. So from a sub control on the page,
there isn't any buildin means to find the component. The only way is if we
know the web page's page class and explicitly access the timer through the
page's member. For example, we define a common base page class which has a
timer member field , then, we can access all those pages derived from the
base page as the base page class instance.

2. SINCE ASP.NET web page is request/response based, so the page's
lifecycle is only between the request comming to the server and the
respsonse be returned to client. Also, any components on the page also only
exist during the page's lifecycle. So I think use a timer at page level
won't work as what we expect because everytime a page is request, a new
timer is constructed and when the page is rendered out and unloaded , the
timer is also disposed. If you do want to do some repeatedly tasks
everytime a page is loaded on the serverside, or constantly post back a
page after a period of time, you can consider Cor 's suggestion about using
the client javascript "setTimeout" to constanly post back the page.
Also, here is a tech article discussing on building such a custom timer
server controL:

#Build a Custom AutoPostBack ASP.NET Page Timer ServerControl
http://www.eggheadcafe.com/articles/20021006.asp


In addition, in asp.net it's ok for us to build a Application scope
background timer which execute over the application's lifecycle and do some
background schedule tasks,here is also a blog thread discussing on this:

#Scheduled Execution in ASP.NET
http://weblogs.asp.net/ashben/archive/2003/10/11/31579.aspx

Also, I've also tried build a appliation level background and store it in
ApplicationState collection, that also works, for example:

protected void Application_Start(Object sender, EventArgs e)
{
System.Threading.Timer timer1 = new System.Threading.Timer(new
System.Threading.TimerCallback(this.CheckStatus),null,0,3000);
Application["timer"] = timer1;
}

Hope helps..


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Martin,

Have you had a chance to check out the suggestios in the former messages or
have you got any further ideas on this issue? If there are anything else we
can help, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top