Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > UserControl run Javascript

Reply
Thread Tools

UserControl run Javascript

 
 
TCORDON
Guest
Posts: n/a
 
      10-18-2005
How can I make a UserControl run some javascript when it loads? this is so
the control sets focus to a textbox it contains.

TIA!


 
Reply With Quote
 
 
 
 
Ken Dopierala Jr.
Guest
Posts: n/a
 
      10-18-2005
Hi,

This will do it, just add it to the load event of your User Control:

If (Page.IsClientScriptBlockRegistered("Focus") = False) Then
MyBase.Page.RegisterStartupScript("Focus", "<script
language='javascript'>document.getElementById('" + TextBox1.ClientID +
"').focus();</script>")
End If

Be sure to use control.ClientID and not just hard code the control id
because ASP.Net will change the control id inside a User Control to somthing
like "_ctl0_controlid". Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"TCORDON" <(E-Mail Removed)> wrote in message
news:OIvr$r$(E-Mail Removed)...
> How can I make a UserControl run some javascript when it loads? this is so
> the control sets focus to a textbox it contains.
>
> TIA!
>
>



 
Reply With Quote
 
 
 
 
TCORDON
Guest
Posts: n/a
 
      10-18-2005
I added de code you gave me, but I dont know if I am doing something wrong,
the focus is not being set. This is how my conde looks now:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

If (Page.IsClientScriptBlockRegistered("Focus") = False) Then

MyBase.Page.RegisterStartupScript("Focus",
"<scriptlanguage='javascript'>document.getElementB yId('" + txtLinea.ClientID
+ "').focus();</script>")

End If

End Sub


"Ken Dopierala Jr." <(E-Mail Removed)> wrote in message
news:uOu6Vz$(E-Mail Removed)...
> Hi,
>
> This will do it, just add it to the load event of your User Control:
>
> If (Page.IsClientScriptBlockRegistered("Focus") = False) Then
> MyBase.Page.RegisterStartupScript("Focus", "<script
> language='javascript'>document.getElementById('" + TextBox1.ClientID +
> "').focus();</script>")
> End If
>
> Be sure to use control.ClientID and not just hard code the control id
> because ASP.Net will change the control id inside a User Control to
> somthing
> like "_ctl0_controlid". Good luck! Ken.
>
> --
> Ken Dopierala Jr.
> For great ASP.Net web hosting try:
> http://www.webhost4life.com/default.asp?refid=Spinlight
> If you sign up under me and need help, email me.
>
> "TCORDON" <(E-Mail Removed)> wrote in message
> news:OIvr$r$(E-Mail Removed)...
>> How can I make a UserControl run some javascript when it loads? this is
>> so
>> the control sets focus to a textbox it contains.
>>
>> TIA!
>>
>>

>
>



 
Reply With Quote
 
Ken Dopierala Jr.
Guest
Posts: n/a
 
      10-18-2005
Hi,

There needs to be a space here between "script" and "language" :

> "<scriptlanguage='javascript'>document.getElementB yId('" +

txtLinea.ClientID

If it still doesn't work then do a view source on your page and post the
outputted HTML. It'll be just above the clsoing </form> tag. Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"TCORDON" <(E-Mail Removed)> wrote in message
news:exQGg6$(E-Mail Removed)...
> I added de code you gave me, but I dont know if I am doing something

wrong,
> the focus is not being set. This is how my conde looks now:
>
> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Load
>
> 'Put user code to initialize the page here
>
> If (Page.IsClientScriptBlockRegistered("Focus") = False) Then
>
> MyBase.Page.RegisterStartupScript("Focus",
> "<scriptlanguage='javascript'>document.getElementB yId('" +

txtLinea.ClientID
> + "').focus();</script>")
>
> End If
>
> End Sub
>
>
> "Ken Dopierala Jr." <(E-Mail Removed)> wrote in message
> news:uOu6Vz$(E-Mail Removed)...
> > Hi,
> >
> > This will do it, just add it to the load event of your User Control:
> >
> > If (Page.IsClientScriptBlockRegistered("Focus") = False) Then
> > MyBase.Page.RegisterStartupScript("Focus", "<script
> > language='javascript'>document.getElementById('" + TextBox1.ClientID +
> > "').focus();</script>")
> > End If
> >
> > Be sure to use control.ClientID and not just hard code the control id
> > because ASP.Net will change the control id inside a User Control to
> > somthing
> > like "_ctl0_controlid". Good luck! Ken.
> >
> > --
> > Ken Dopierala Jr.
> > For great ASP.Net web hosting try:
> > http://www.webhost4life.com/default.asp?refid=Spinlight
> > If you sign up under me and need help, email me.
> >
> > "TCORDON" <(E-Mail Removed)> wrote in message
> > news:OIvr$r$(E-Mail Removed)...
> >> How can I make a UserControl run some javascript when it loads? this is
> >> so
> >> the control sets focus to a textbox it contains.
> >>
> >> TIA!
> >>
> >>

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting a property of a control inside a usercontrol, from another usercontrol Fabiano ASP .Net 2 06-03-2005 09:56 AM
accessing usercontrol from another usercontrol Phl ASP .Net 2 11-18-2004 07:33 PM
Access a control inside an usercontrol from another control inside another usercontrol nail ASP .Net 0 09-15-2004 03:55 PM
Can we use a usercontrol inside a usercontrol Rajesh Tiwari ASP .Net 0 08-12-2003 03:56 PM
Use LoadControl to load a usercontrol but the webcontrol in the usercontrol can not AutoPostBack huobazi ASP .Net 1 07-03-2003 03:14 AM



Advertisments