This is a migrated thread and some comments may be shown as answers.

[Solved] Updated client control reference after Ajax call

2 Answers 204 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
John Fetherolf
Top achievements
Rank 1
John Fetherolf asked on 09 Jan 2008, 04:28 PM

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

2 Answers, 1 is accepted

Sort by
0
John Fetherolf
Top achievements
Rank 1
answered on 09 Jan 2008, 05:46 PM
After thinking about it, I wanted to clarify my question a bit.

What I have on the page is basically a page-scope variable called editCustNo that is used from several JavaScript functions.  This variable is initialized on Page_Load by the RegisterClientPageControls() C# method in my example, so when the page initially loads it contains a line of code:

var editCustNo = document.getElementById("ctl00_ContentPlaceHolder1_editCustNo"); 

This variable is really a convenience that is autogenerated on the server so I don't have to use asp <%%> tags in the markup or hard code the ugly mangled control ClientID anywhere in the JavaScript.

After the Ajax call, my variable that I set in Page_Load is no longer valid for some reason and I would like to know how to reset the variable to the correct (new ???) document element that was generated by the Ajax update.

Hope this clears my original post up a bit.

Thanks,
Kevin
0
John Fetherolf
Top achievements
Rank 1
answered on 10 Jan 2008, 09:07 PM

Nevermind on this.  I figured out my problem.  The issue was the following line of code:

                    sb.AppendFormat("var {0} = document.getElementById('{1}');\n", ctrl.ID, ctrl.ClientID);

By including the "var" in the response script I am setting a variable local to the respose script instead of my page-scope variable declared during Page_Load.  Stupid JavaScript error on my part.

Thanks as always for the help,
Kevin

Tags
Ajax
Asked by
John Fetherolf
Top achievements
Rank 1
Answers by
John Fetherolf
Top achievements
Rank 1
Share this question
or