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" >

<HTML>

           <HEAD>

                      <title>WebForm2</title>

                      <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="http://schemas.microsoft.com/intellisense/ie5">

           </HEAD>

           <body MS_POSITIONING="FlowLayout">

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

                                 <P>

                                            <table>

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

                                                                 <ItemTemplate>

                                                                            <tr>

                                                                                       <td>

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

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

                                                                                       </td>

                                                                            </tr>

                                                                 </ItemTemplate>

                                                      </asp:Repeater>

                                            </table>

                                 </P>

                      </form>

           </body>

</HTML>

 

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)

                      {

                                 this.id = 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)

                      {

                                 FillArrayList();

 

                                 if(!IsPostBack)

                                            BindData();

                      }

 

                      private void BindData()

                      {

                                 Repeater1.DataSource = this.ht;

                                 Repeater1.DataBind();

                      }

 

                      private void FillArrayList()

                      {

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

                                            this.ht.Add(new somedata("id"+i,"value"+i));

                      }

 

 

                      protected void Repeater1_ItemCommand(object sender, RepeaterCommandEventArgs e)

                      {

                                 if(e.CommandSource is WebControl)

                                            SetControlFocus(e.CommandSource);

                      }

 

                      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("}");

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

                                 sb.Append("</script>");

                                 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.

                                 //

                                 InitializeComponent();

                                 base.OnInit(e);

                      }

                     

                      /// <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);

 

                      }

                      #endregion

           }

}

 

 

 

No Comments