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