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

5 posts, 0 answers
  1. Rebecca Rasmussen
    Rebecca Rasmussen avatar
    20 posts
    Member since:
    Mar 2007

    Posted 27 Feb 2009 Link to this post

    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!

  2. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 04 Mar 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rebecca Rasmussen
    Rebecca Rasmussen avatar
    20 posts
    Member since:
    Mar 2007

    Posted 04 Mar 2009 Link to this post

    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


    1 using System;  
    2 using System.Collections;  
    3 using System.Configuration;  
    4 using System.Data;  
    5 using System.Linq;  
    6 using System.Web;  
    7 using System.Web.Security;  
    8 using System.Web.UI;  
    9 using System.Web.UI.HtmlControls;  
    10 using System.Web.UI.WebControls;  
    11 using System.Web.UI.WebControls.WebParts;  
    12 using System.Xml.Linq;  
    13  
    14 public partial class CustomControls_other_submit_feedback : myBaseUserControl // System.Web.UI.UserControl  
    15 {  
    16     protected string mySentryClass = "feedback.ascx";
    17
    18     #region -- Page Load --  
    19     public void Page_Load(object sender, EventArgs e)  
    20     {  
    21         try 
    22         {  
    23             if (!Page.IsPostBack) TextBox_additionalComments.ReadOnly = false;  
    24  
    25             if (!TextBox_additionalComments.ReadOnly)  
    26             {  
    27                 Telerik.Web.UI.RadAjaxManager myManager = Telerik.Web.UI.RadAjaxManager.GetCurrent(Page);  
    28                 myManager.FocusControl(TextBox_additionalComments);  
    29             }  
    30         }  
    31         catch (System.Exception sysEx)  
    32         {  
    33             Label_message.Text = myApplicationException.handleIt(sysEx, mySentryClass);  
    34             Label_message.CssClass = "systemError";  
    35         }  
    36     }
    37     #endregion -- Page Load --  
    38  
    39     #region -- Button: Submit Feedback --  
    40     protected void Button_submitFeedback_Click(object sender, EventArgs e)  
    41     {  
    42         try 
    43         {  
    44             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();   
    45  
    46             if (myIdentityValue == null || myIdentityValue == "")  
    47             {  
    48                 Label_message.Text = "Unable to save your feedback to the database. Please e-mail to " + System.Web.HttpContext.Current.Application["APP_application_email"].ToString();  
    49                 Label_message.CssClass = "systemError";  
    50                 Button_submitFeedback.Enabled = false;  
    51                 TextBox_additionalComments.ReadOnly = true;  
    52                 TextBox_additionalComments.BackColor = System.Drawing.Color.BlanchedAlmond;  
    53             }  
    54             else 
    55             {  
    56                 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!";  
    57                 Label_message.CssClass = "successfulCompletion";  
    58                 Button_submitFeedback.Enabled = false;  
    59                 TextBox_additionalComments.ReadOnly = true;  
    60                 TextBox_additionalComments.BackColor = System.Drawing.Color.Gainsboro;  
    61             }  
    62         }  
    63         catch (System.Exception sysEx)  
    64         {  
    65             Label_message.Text = myApplicationException.handleIt(sysEx, mySentryClass);  
    66             Label_message.CssClass = "systemError";  
    67         }  
    68     }
    69     #endregion -- Button: Submit Feedback --  
    70
    1 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="submit_feedback.ascx.cs" Inherits="CustomControls_other_submit_feedback" %> 
    2  
    3 <strong>Input your comments below, then click the 'Submit Feedback' button.</strong><br /> 
    4 <asp:TextBox ID="TextBox_additionalComments" runat="server" Columns="60" MaxLength="2500" 
    5 Rows="10" TextMode="MultiLine" BackColor="#FFFAF0"></asp:TextBox> 
    6 <br /> 
    7 <asp:Button ID="Button_submitFeedback" runat="server" OnClick="Button_submitFeedback_Click" 
    8 Text="Submit Feedback" /> 
    9 <br /><br /> 
    10 <asp:Label ID="Label_message" runat="server"></asp:Label> 


  5. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 07 Mar 2009 Link to this post

    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.
  6. Rebecca Rasmussen
    Rebecca Rasmussen avatar
    20 posts
    Member since:
    Mar 2007

    Posted 23 Nov 2009 Link to this post

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

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017