I am having problems with trying to set focus to a textbox control after an Ajax call. Here is the rub, the control is marked as being updated in the call. The error message I keep getting is "Unexpected call to method or property access".
This is using AjaxManagerProxy from a content page as follows.
| <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"></asp:ScriptManagerProxy> |
| <telerik:RadAjaxManagerProxy ID="RadAjaxManager1" runat="server"> |
| <AjaxSettings> |
| <telerik:AjaxSetting AjaxControlID="btnReset"> |
| <UpdatedControls> |
| <telerik:AjaxUpdatedControl ControlID="editCustNo" /> |
| </UpdatedControls> |
| </telerik:AjaxSetting> |
| </AjaxSettings> |
| </telerik:RadAjaxManagerProxy> |
The server button handler looks like this:
| protected void btnReset_Click(object sender, EventArgs e) { |
| // Reset the page controls to their default state. |
| editCustNo.Text = string.Empty; |
| RegisterClientPageControls(editCustNo); |
| _RadAjaxMgr.ResponseScripts.Add("editCustNo.focus();"); |
| } |
| protected void RegisterClientPageControls(params Control[] controls) { |
| Type typ = this.GetType().BaseType; |
| StringBuilder sb = new StringBuilder(); |
| sb.AppendLine(); |
| sb.AppendLine(); |
| foreach(Control ctrl in controls) |
| if(ctrl != null) |
| sb.AppendFormat("var {0} = document.getElementById('{1}');\n", ctrl.ID, ctrl.ClientID); |
| sb.AppendLine(); |
| if(IsAjaxRequest == true) { |
| _RadAjaxMgr.ResponseScripts.Add(sb.ToString()); |
| return; |
| } |
| this.ClientScript.RegisterStartupScript(typ, Guid.NewGuid().ToString(), sb.ToString(), true); |
| } |
Any reason why this shouldn't work? I have another version where I replace this line
| _RadAjaxMgr.ResponseScripts.Add("editCustNo.focus();"); |
| // ... with ... |
| _RadAjaxMgr.ResponseScripts.Add(string.Format(SetFocus('{0}');", editCustNo.ClientID)); |
Where SetFocus() is a JavaScript function like this:
| function SetFocus(elementName) { |
| var ctrl = document.getElementById(elementName); |
| if(ctrl.focus) |
| ctrl.focus(); |
| } |
... which works like a champ.
So I guess the question is why doesn't the first method work? I would seem to me that the two methods are functionally equivalent. Also this is only a very simple example. I realize there is probably a better way to set focus than this, but the bigger issue is how do I get a javascript reference to an Ajaxified control after the Ajax call?
Any help with this would be greatly appreciated.
Thanks,
Kevin