Question

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):

<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 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();
}
Was it helpful?

Solution

When you rebind the repeater on do_whatever(), the textbox is recreated, then you have to find it again.

protected void rptExclPBSA_ItemCommand(object source, RepeaterCommandEventArgs e)
{
  TextBox tbExclBox = (TextBox)rptExclPBSA.Controls[0].Controls[0].FindControl("tbExclBox");
  do_whatever()

  tbExclBox = (TextBox)rptExclPBSA.Controls[0].Controls[0].FindControl("tbExclBox");
  tbExclBox.Focus();
}

the event ItemDataBound is not required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top