Note that there are some explanatory texts on larger screens.

plurals
  1. POSet focus to texbox in repeater
    primarykey
    data
    text
    <p>I have a repeater with header and item templates. The header contains a textbox and link button ("Add" to add the item entered in textbox to the list). What I need is to be able to set the focus back on the textbox after "Add" is clicked. I am including the code and what I have tried (to no avail). I include OnItemDataBound for repeaer, a javascript to set the focus (want to do this client-side):</p> <pre><code>&lt;asp:Repeater runat="server" ID="rptExclPBSA" OnItemDataBound="rptExclPBSA_ItemDataBound" OnItemCommand="rptExclPBSA_ItemCommand"&gt; &lt;HeaderTemplate&gt; &lt;table style="width:300px" border="0"&gt; &lt;tr&gt; &lt;td style="vertical-align:top;width:100px"&gt; &lt;asp:TextBox runat="server" ID="tbExclBox" CssClass="NormalSmall" Width="90" MaxLength="5" /&gt; &lt;/td&gt; &lt;td style="width:200px;"&gt; &lt;asp:LinkButton ID="lbAddExcl" runat="server" CommandName="Add" Text="Add Something" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;table style="width:300px" border="0"&gt; &lt;tr&gt; &lt;td style="vertical-align:top;width:100px;text-align:center" class="NormalSmall"&gt; &lt;%# Eval("Box") %&gt; &lt;/td&gt; &lt;td style="vertical-align:top;width:200px;"&gt; &lt;asp:ImageButton ID="ibRemoveExcl" runat="server" ImageUrl="images/delete.gif" CommandName="Remove" AlternateText="Delete That Thing" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/ItemTemplate&gt; &lt;/asp:Repeater&gt; </code></pre> <p>In code behind:</p> <pre><code>protected void rptExclPBSA_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Header) { LinkButton lbAddExcl = e.Item.FindControl("lbAddExcl") as LinkButton; TextBox tbExclBox = e.Item.FindControl("tbExclBox") as TextBox; if (null != lbAddExcl &amp;&amp; null != tbExclBox) lbAddExcl.Attributes.Add("onclick", "setFocusPOB('" + tbExclBox.ClientID + "');"); } } protected void rptExclPBSA_ItemCommand(object source, RepeaterCommandEventArgs e) { TextBox tbExclBox = (TextBox)rptExclPBSA.Controls[0].Controls[0].FindControl("tbExclBox"); do_whatever() tbExclBox.Focus(); } </code></pre> <p>Javascript:</p> <pre><code>function setFocusPOB(ctrl_id){ var tbExclBox = document.getElementById(ctrl_id); if (null != tbExclBox) tbExclBox.focus(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload