Set focus on a clicked control in ASP.NET (part 2)

I got a message from some friends that the code sample I had in my previous post didn't work when the control was located within a repeater or a datalist/datagrid, so I sat down and changed the code somewhat. The problem with controls in a repeater is that the container control eats up the event so the OnBubbleEvent() never fires.

Now, the trick is to do exactly the same thing, but in the OnItemCommand event fired by the repeater/datalist/datagrid instead.

So, the ASP-Page could look something like this:

<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="ControlTest.WebForm2" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >




                      <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

                      <meta name="CODE_LANGUAGE" Content="C#">

                      <meta name="vs_defaultClientScript" content="JavaScript">

                      <meta name="vs_targetSchema" content="">


           <body MS_POSITIONING="FlowLayout">

                      <form id="Form1" method="post" runat="server">



                                                      <asp:Repeater id="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">




                                                                                                  <ASP:Button ID="someID" CommandName="someCommand" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "id") %>'

                                                                                                  Text='<%# DataBinder.Eval(Container.DataItem, "id") %>' runat="server" />











And the code behind for this simple sample:

using System;
using System.Collections;
using System.Diagnostics;
using System.Text;
using System.Web.UI.WebControls;

namespace ControlTest


           public class somedata


                      public somedata(string id, string value)


                        = id;

                                 this.value = value;



                      private string id;

                      private string value;


                      public string Id


                                 get { return id; }

                                 set { id = value; }



                      public string Value


                                 get { return value; }

                                 set { this.value = value; }



           /// <summary>

           /// Summary description for WebForm2.

           /// </summary>

           public class WebForm2 : System.Web.UI.Page


                      protected System.Web.UI.WebControls.Repeater Repeater1;

                      private ArrayList ht = new ArrayList();


                      private void Page_Load(object sender, System.EventArgs e)








                      private void BindData()


                                 Repeater1.DataSource =;




                      private void FillArrayList()


                                 for(int i = 0; i<10; i++)





                      protected void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)


                                 if(e.CommandSource is WebControl)




                      private void SetControlFocus(object sender)


                                 WebControl webControl = (WebControl)sender;

                                 Debug.WriteLine("Clicked " + webControl.ClientID);

                                 StringBuilder sb = GetFocusScriptBlock(webControl);

                                 RegisterClientScriptBlock("FocusScript", sb.ToString());



                      private static StringBuilder GetFocusScriptBlock(WebControl webControl)


                                 StringBuilder sb = new StringBuilder(1000);

                                 sb.Append("<script language = \"javascript\">");

                                 sb.Append("function ControlFocus() {");

                                 sb.Append("document.getElementById('" + webControl.ClientID + "').focus();");


                                 sb.Append(String.Concat(Environment.NewLine, Environment.NewLine, "window.onload = ControlFocus;"));


                                 return sb;



                      #region Web Form Designer generated code

                      override protected void OnInit(EventArgs e)



                                 // CODEGEN: This call is required by the ASP.NET Web Form Designer.






                      /// <summary>

                      /// Required method for Designer support - do not modify

                      /// the contents of this method with the code editor.

                      /// </summary>

                      private void InitializeComponent()


                                 this.Load += new System.EventHandler(this.Page_Load);









No Comments