How do I set focus in .ascx from .aspx page?http://forums.asp.net/t/1794435.aspx/1?How+do+I+set+focus+in+ascx+from+aspx+page+Thu, 19 Apr 2012 22:00:21 -040017944354939782http://forums.asp.net/p/1794435/4939782.aspx/1?How+do+I+set+focus+in+ascx+from+aspx+page+How do I set focus in .ascx from .aspx page? <p>I have a bulletin.aspx page that includes an imagePicker.ascx control (allows user to pick a picture). &nbsp;Based on certain situations, I would like to set the focus to be a dropdown within the imagePicker.ascx control. &nbsp;</p> <p>I use a Session variable to track what control to focus on. &nbsp;The Session var is being set correctly, and the javascript value appears to be set (I can see it in an alert box), but the focus just does not work for the imagePicker.ascx.</p> <p>Any help would be greatly appreciated. &nbsp;Here's the code:</p> <pre class="prettyprint">&lt;script&gt; var focus = '&lt;%= Session[&quot;focus&quot;] %&gt;'; var TextBoxBulletinTo=document.getElementById(&quot;&lt;%= TextBoxBulletinTo.ClientID %&gt;&quot;); var ImageLoader = document.getElementById(&quot;&lt;%= ImageLoader1.DropDownListAlbumClientID %&gt;&quot;); function CuteEditor_OnInitialized(editor) { setTimeout(focusControl,100); } function focusControl() { if(focus==&quot;default&quot;) { TextBoxBulletinTo.focus(); } if(focus==&quot;image&quot;) {</pre> <pre class="prettyprint">&nbsp; &nbsp; &nbsp; &nbsp; alert(focus); ImageLoader.focus(); } } &lt;/script&gt;</pre> <p>And in my imagePicker.ascx:</p> <p>public string DropDownListAlbumClientID<br> {<br> get { return DropDownListAlbum.ClientID; }<br> }</p> <p><br> <br> </p> 2012-04-18T23:24:34-04:004940122http://forums.asp.net/p/1794435/4940122.aspx/1?Re+How+do+I+set+focus+in+ascx+from+aspx+page+Re: How do I set focus in .ascx from .aspx page? <p>Though image control will have focus but it cannot be seen as we can see the focus(cursor blinking) in textbox control.</p> 2012-04-19T05:44:32-04:004941147http://forums.asp.net/p/1794435/4941147.aspx/1?Re+How+do+I+set+focus+in+ascx+from+aspx+page+Re: How do I set focus in .ascx from .aspx page? <p>But I am trying to set focus to the DropDownList in the imagePicker.ascx.</p> 2012-04-19T13:18:06-04:004941148http://forums.asp.net/p/1794435/4941148.aspx/1?Re+How+do+I+set+focus+in+ascx+from+aspx+page+Re: How do I set focus in .ascx from .aspx page? <p>&nbsp;I am not trying to set focus to the imagePicker.ascx.</p> 2012-04-19T13:18:21-04:004941167http://forums.asp.net/p/1794435/4941167.aspx/1?Re+How+do+I+set+focus+in+ascx+from+aspx+page+Re: How do I set focus in .ascx from .aspx page? <p>In page load event of your user control registered jaavascript with caling one javascript function and pass your related control's client id and in this js function as below.</p> <pre class="prettyprint">function Setfocus(control) { document.getElementByID(control).focus(); }</pre> <p>because user control's control have differnet id so you have to pass it from user control's load event, to get accurate it.</p> <p><br> <br> </p> 2012-04-19T13:24:03-04:004941837http://forums.asp.net/p/1794435/4941837.aspx/1?Re+How+do+I+set+focus+in+ascx+from+aspx+page+Re: How do I set focus in .ascx from .aspx page? <p>Many thanks for the suggestions. &nbsp;Basically, I was doing all this work to prevent the HTML Editor from stealing the focus.</p> <p>Because the .ascx has the control I need to focus, I was required to use a Public variable to access that control. &nbsp;Once I had that properly working, I was able to force the focus from either the .aspx or .ascx using javascript and a session var.</p> <p>Here is a final sample of the code that work:</p> <pre class="prettyprint">-- .aspx.cs public partial class testCE : System.Web.UI.Page { protected override void OnLoad(EventArgs e) { if (!IsPostBack) { Session[&quot;focus&quot;] = &quot;default&quot;; } base.OnLoad(e); } protected void Page_Load(object sender, EventArgs e) { } } -- .aspx &lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;testCE.aspx.cs&quot; Inherits=&quot;testCE&quot; %&gt; &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt; &lt;%@ Register TagPrefix=&quot;CE&quot; Namespace=&quot;CuteEditor&quot; Assembly=&quot;CuteEditor&quot; %&gt; &lt;%@ Register Src=&quot;TestUserControl.ascx&quot; TagName=&quot;TestUserControl&quot; TagPrefix=&quot;uc1&quot; %&gt; &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt; &lt;head runat=&quot;server&quot;&gt; &lt;title&gt;Untitled Page&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt; &lt;div&gt; &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt; &lt;br /&gt; &lt;CE:Editor Width=&quot;505&quot; Height=&quot;150&quot; ID=&quot;TextBoxBulletinDescription&quot; runat=&quot;server&quot; EnableContextMenu=&quot;true&quot; EditorOnPaste=&quot;PasteWord&quot; ShowBottomBar=&quot;false&quot; MaxHTMLLength=&quot;7000&quot; Focus=&quot;false&quot;&gt;&lt;/CE:Editor&gt; &lt;br /&gt; &lt;uc1:TestUserControl ID=&quot;TestUserControl&quot; runat=&quot;server&quot;&gt;&lt;/uc1:TestUserControl&gt; &lt;/div&gt; &lt;/form&gt; &lt;script&gt; var focus = '&lt;%= Session[&quot;focus&quot;] %&gt;'; var TextBox1=document.getElementById(&quot;&lt;%= TextBox1.ClientID %&gt;&quot;); var TestUserControlDDL = document.getElementById(&quot;&lt;%= TestUserControl.DropDownListClientID %&gt;&quot;); function CuteEditor_OnInitialized(editor) { setTimeout(focusControl,100); } function focusControl() { if(focus==&quot;default&quot;) { TextBox1.focus(); } if(focus==&quot;ddl&quot;) { TestUserControlDDL.focus(); } } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; -- USER CONTROL-- --.ascx.cs 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 TestUserControl : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Session[&quot;focus&quot;] = &quot;ddl&quot;; Label1.Text = &quot;Selected value = &quot; &#43; DropDownList1.SelectedValue.ToString(); } public string DropDownListClientID { get { return DropDownList1.ClientID; } } } -- .ascx &lt;%@ Control Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeFile=&quot;TestUserControl.ascx.cs&quot; Inherits=&quot;TestUserControl&quot; %&gt; &lt;asp:DropDownList ID=&quot;DropDownList1&quot; runat=&quot;server&quot; AutoPostBack=&quot;true&quot; onselectedindexchanged=&quot;DropDownList1_SelectedIndexChanged&quot;&gt; &lt;asp:ListItem&gt;Option 1&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;Option 2&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;br /&gt; &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&quot;&quot;&gt;&lt;/asp:Label&gt;</pre> <p><br> <br> </p> 2012-04-19T22:00:21-04:00