Problem with Page.SetFocus with AJAX extenders

K

kurt sune

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
 
B

bruce barker

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)
 
K

kurt sune

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
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top