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

How Do I ... SetFocus to a control w/i a Web User Control from C# Code-Behind

4 Answers 134 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Rebecca Rasmussen
Top achievements
Rank 2
Rebecca Rasmussen asked on 27 Feb 2009, 01:22 PM
Ah, this used to be sooooooooooo easy ... place a RadAjaxManager in the control then use the SetFocus() method to move the focus around the web user controls as needed.

Now that only one RadAjaxManager can exist per .aspx page, this has become rather a nightmare. For me at least. And I've searched and searched and none of the solutions mentioned have worked for me.

I'm hopeful though. :)

  • Scenario: I've a default.aspx page. Kinda like a master page I suppose, but it is not a "Master Page". When I call this default.aspx page, I pass in a menuId value via the query string. Based on that menuId value, I load a particular web user control ascx into a placeholder on that .aspx. The .aspx page contains the RadScriptManager and the RadAjaxManager.
  • Constraint: I have to be able to set the focus at will via the C# code behind, from within any given loaded web user control, after any page load, post back, or ajax request. (Oh and please do not refer me to client-side examples; I've seen some here, and just can't manage to do the translation from client-side to code-behind from w/i a web user control; I need a translator :0) )
  • Would Like: a reusable solution as I do not want to have to recreate a chunk of code or javascript on each user control in general principle not to mention (though I am mentioning it aren't I?) to accomplish what used to be an oh so very simple one-liner.

Thanks in advance for any suggestions!

4 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 04 Mar 2009, 12:39 PM
Hello Rebecca,

You can easily access the RadAjaxManager located in the default.aspx page from the code behind of any user control, just try the GetCurrent() static method of RadAjaxManager:

WebUserControl.ascx.cs
RadAjaxManager manager = RadAjaxManager.GetCurrent(Page); 

Find more information here.

Let me know if this helps.

All the best,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Rebecca Rasmussen
Top achievements
Rank 2
answered on 04 Mar 2009, 12:53 PM
Have tried that. The RadManager instance is created, lets me access the FocusControl method, I get no error, but it does not set the focus. :( I'm sure I'm missing something fundamentally obvious, but it's been one of those years ... :p


using System;  
using System.Collections;  
using System.Configuration;  
using System.Data;  
using System.Linq;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.HtmlControls;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Xml.Linq;  
 
public partial class CustomControls_other_submit_feedback : myBaseUserControl // System.Web.UI.UserControl  
{  
    protected string mySentryClass = "feedback.ascx";
    #region -- Page Load --  
    public void Page_Load(object sender, EventArgs e)  
    {  
        try 
        {  
            if (!Page.IsPostBack) TextBox_additionalComments.ReadOnly = false;  
 
            if (!TextBox_additionalComments.ReadOnly)  
            {  
                Telerik.Web.UI.RadAjaxManager myManager = Telerik.Web.UI.RadAjaxManager.GetCurrent(Page);  
                myManager.FocusControl(TextBox_additionalComments);  
            }  
        }  
        catch (System.Exception sysEx)  
        {  
            Label_message.Text = myApplicationException.handleIt(sysEx, mySentryClass);  
            Label_message.CssClass = "systemError";  
        }  
    }
    #endregion -- Page Load --  
 
    #region -- Button: Submit Feedback --  
    protected void Button_submitFeedback_Click(object sender, EventArgs e)  
    {  
        try 
        {  
            string myIdentityValue = myDatabaseAccess.v2_sp_application_feedback_data("%", TextBox_additionalComments.Text.Trim(), "%""%""New", myUserInfo.mn_mnet_id, myUserInfo.role_name, "submit").Rows[0][0].ToString();   
 
            if (myIdentityValue == null || myIdentityValue == "")  
            {  
                Label_message.Text = "Unable to save your feedback to the database. Please e-mail to " + System.Web.HttpContext.Current.Application["APP_application_email"].ToString();  
                Label_message.CssClass = "systemError";  
                Button_submitFeedback.Enabled = false;  
                TextBox_additionalComments.ReadOnly = true;  
                TextBox_additionalComments.BackColor = System.Drawing.Color.BlanchedAlmond;  
            }  
            else 
            {  
                Label_message.Text = "Your feedback has been successfully saved to the database (reference id = " + myIdentityValue + ").<br/><br/><b>Thank-you</b> for taking the time to let us know how you feel!";  
                Label_message.CssClass = "successfulCompletion";  
                Button_submitFeedback.Enabled = false;  
                TextBox_additionalComments.ReadOnly = true;  
                TextBox_additionalComments.BackColor = System.Drawing.Color.Gainsboro;  
            }  
        }  
        catch (System.Exception sysEx)  
        {  
            Label_message.Text = myApplicationException.handleIt(sysEx, mySentryClass);  
            Label_message.CssClass = "systemError";  
        }  
    }
    #endregion -- Button: Submit Feedback --  
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="submit_feedback.ascx.cs" Inherits="CustomControls_other_submit_feedback" %> 
 
<strong>Input your comments below, then click the 'Submit Feedback' button.</strong><br /> 
<asp:TextBox ID="TextBox_additionalComments" runat="server" Columns="60" MaxLength="2500" 
Rows="10" TextMode="MultiLine" BackColor="#FFFAF0"></asp:TextBox> 
<br /> 
<asp:Button ID="Button_submitFeedback" runat="server" OnClick="Button_submitFeedback_Click" 
Text="Submit Feedback" /> 
<br /><br /> 
<asp:Label ID="Label_message" runat="server"></asp:Label> 


0
Iana Tsolova
Telerik team
answered on 07 Mar 2009, 09:16 AM
Hello Rebecca,

I went through your code and it looks ok. However, I suggest that you move the code with the FocusControl to Page_PreRender instead of calling it on Page_Load. Check it out and let me know if it makes any difference. 
Additionally, what other controls are on the problematic page and is this user control part of ajaxified content?
Any further information might be helpful in finding out what could have gone wrong.

Best wishes,
Iana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Rebecca Rasmussen
Top achievements
Rank 2
answered on 23 Nov 2009, 10:04 PM
Been a looooong time since I've had the change to look at this issue in my project. But, just upgraded the control suite to 

2009.3.1103.35 and now setting the focus in a user control using the below in any part of my code is working. :) Yeeeeeeeeeeha!

Telerik.Web.UI.RadAjaxManager myManager = Telerik.Web.UI.RadAjaxManager.GetCurrent(Page);   
myManager.FocusControl(TextBox_additionalComments); 

I've even shifted the "Telerik.Web.UI.RadAjaxManager myManager = Telerik.Web.UI.RadAjaxManager.GetCurrent(Page); " part into my base user control class and can call the "myManager.FocusControl(TextBox_additionalComments);" from anywhere. :)

Tags
Ajax
Asked by
Rebecca Rasmussen
Top achievements
Rank 2
Answers by
Iana Tsolova
Telerik team
Rebecca Rasmussen
Top achievements
Rank 2
Share this question
or