简体   繁体   中英

Textbox focus in asp updatepanel - Working with breakpoint but not without

I'm having a really strange problem where my code works with a breakpoint set in the code, but when I remove the breakpoint, parts of the code stop functioning.

I'm trying to make a textbox select all text on focus; I want it to automatically focus, selecting all text, when a search is performed.

I have a texbox (groupSearchTextbox), button (groupSearchButton), and a listbox which is inside of an update panel, inside of a panel with a default button specified:

<asp:Panel ID="groupPanel" runat="server" CssClass="listContainer" DefaultButton="groupSearchButton">
    <h2>User Groups</h2>
    <div class="searches">
        <asp:TextBox ID="groupSearchTextbox" runat="server"></asp:TextBox>
        <asp:Button ID="groupSearchButton" runat="server" Text="Search" OnClick="groupSearchButton_Click" />
        <asp:Button ID="showAllGroupsButton" runat="server" Text="Show All" OnClick="showAllGroupsButton_Click" CssClass="right" />
    </div>
    <asp:UpdatePanel ID="groupUpdate" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:ListBox ID="groupListbox" runat="server" CssClass="list"></asp:ListBox>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="groupSearchButton" EventName="Click" />
            <asp:AsyncPostBackTrigger ControlID="showAllGroupsButton" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>
</asp:Panel>

I also have a jquery function to make the textboxes select on focus:

$('.searches input[type=text]').focus(function () {
        $(this).select();
    });

and onclick of groupSearchButton I have the following function to remove items from the listbox if they are not results of a search, and focus on the textbox:

protected void groupSearchButton_Click(object sender, EventArgs e) {
    fillGroups(); //Sets the listbox to the original list
    string searchString = groupSearchTextbox.Text;`

    for (int i = groupListbox.Items.Count - 1; i > 0; i--) {
        string itemName = groupListbox.Items[i].ToString();
        if (!itemName.ToLower().Contains(searchString.ToLower())) {
            groupListbox.Items.Remove(groupListbox.Items[i]);
        }
    }
    groupSearchTextbox.Focus();
}

When I click the groupSearchButton , everything works as expected. I get my results and the groupSearchTextbox gets focus with the text selected. When I press enter while focused on the textbox, making use of the panel's default button attribute, I get my results but the text in the textbox is not selected.

The strange part is, if I put a breakpoint where I set the focus in the groupSearchButton_Click method, the above method of pressing enter in the textbox works properly and the text is selected.

Any ideas what might be going on here?

Edit: So I'm pretty sure that the issue is that the textbox needs to lose focus in order for it to select the text when it focuses again. That would explain the actual clicking of the button working, as well as (I believe) the breakpoint issue, since the textbox would lose focus when Visual Studio is displayed.

I came up with a fairly hacky jquery fix for this, but would still love to know if there's a decent way to handle this properly

If you want to focus on only one text box then you can use

<asp:Panel ID="groupPanel" runat="server" CssClass="listContainer" DefaultButton="groupSearchButton" defaultfocus="groupSearchTextbox">

on your panel

You can use this in Page_Load :

 protected void Page_Load(object sender, EventArgs e)
    {
         if (IsPostBack)
        {
            groupSearchTextbox.Focus();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM