Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

Problem with Page.SetFocus with AJAX extenders

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kurt sune - 15 Feb 2008 08:53 GMT
I am having trouble with SetFocus.

I have a textbox with AJAX extenders:

<asp:TextBox ID="txtOrg" runat="server" CssClass="inputstyle"
MaxLength="10"></asp:TextBox>

<cc1:TextBoxWatermarkExtender ID="txtOrg_TextBoxWatermarkExtender"

runat="server" Enabled="True" TargetControlID="txtOrg"
WatermarkCssClass="watermarked" WatermarkText="write an  orgnumber">

</cc1:TextBoxWatermarkExtender>

<cc1:FilteredTextBoxExtender ID="txtOrg_FilteredTextBoxExtender"
runat="server"

Enabled="True" TargetControlID="txtOrg" ValidChars="0123456789">

</cc1:FilteredTextBoxExtender>

In page load I do:  SetFocus(txtOrg)

The result: The textbox does not get focus, instead the caret is placed in
the textbox after the watermark text.

If I in the page html in body onload do
document.getElementById('txtOrg').focus();

the the focus is properly set in the textbox.

Ideas anyone?

/k
bruce barker - 15 Feb 2008 16:01 GMT
thats because the behavior is applied after the focus, and the watermark does
not detect it has the focus (the onfocus event has already happened).

you should use the ajax library to set the focus via:

    Sys.Apllication.add_load()

then the behavior will be attached before the focus event.

-- bruce (sqlwork.com)

> I am having trouble with SetFocus.
>
[quoted text clipped - 30 lines]
>
> /k
kurt sune - 21 Feb 2008 10:05 GMT
The solution:

Private Sub SetFocusOnLoad(ByVal control As Control)
If control Is Nothing Then

Throw New ArgumentNullException("control", "Control cannot be null!")
End If

Dim script As String = "(function() { " + "var fn = function() { " + "var
control = $get('" + control.ClientID + "'); " + "if (control &&
control.focus) { control.focus(); } " + "Sys.Application.remove_load(fn);" +
"};" + "Sys.Application.add_load(fn);" + "})();"
ScriptManager.RegisterStartupScript(control.Page, control.[GetType](),
control.ClientID + "_SetFocusOnLoad", script, True)

End Sub

/k

> thats because the behavior is applied after the focus, and the watermark
> does
[quoted text clipped - 7 lines]
>
> -- bruce (sqlwork.com)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.