I am working with a repeater that has header and item templates. The header includes a textbox and a link button labeled "Add" to add the item entered in the textbox to the list. My goal is to set the focus back on the textbox after clicking "Add". Below is the code snippet along with my attempts (which have not been successful). I have also included the OnItemDataBound for the repeater, along with a JavaScript function to set the focus client-side:
<asp:Repeater
runat="server"
ID="rptExclPBSA"
OnItemDataBound="rptExclPBSA_ItemDataBound"
OnItemCommand="rptExclPBSA_ItemCommand">
<HeaderTemplate>
<table style="width:300px" border="0">
<tr>
<td style="vertical-align:top;width:100px">
<asp:TextBox runat="server" ID="tbExclBox" CssClass="NormalSmall" Width="90" MaxLength="5" />
</td>
<td style="width:200px;">
<asp:LinkButton ID="lbAddExcl" runat="server" CommandName="Add" Text="Add Something" />
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table style="width:300px" border="0">
<tr>
<td style="vertical-align:top;width:100px;text-align:center" class="NormalSmall">
<%# Eval("Box") %>
</td>
<td style="vertical-align:top;width:200px;">
<asp:ImageButton ID="ibRemoveExcl" runat="server" ImageUrl="images/delete.gif" CommandName="Remove" AlternateText="Delete That Thing" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
In the code behind:
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 && 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();
}
Javascript:
function setFocusPOB(ctrl_id){
var tbExclBox = document.getElementById(ctrl_id);
if (null != tbExclBox)
tbExclBox.focus();
}