简体   繁体   中英

How to keep focus on the text box after text changed event

I have a gridview which contains a text box as a template field.the grid view is in an updatepanel.

I use the text changed event to to calculate the percentage of the first four textboxes and put the result in the fifth text box, My problem is: i always lose the focus as soon as the text changed,and every time i was supposed to move the mouse cursor again to the target text box.How to solve this problem?I wanna to keep the focus on my text box after text changed.

My Code:

 private void calc()
        {
            float sum = 0;
            for (int i = 0; i < 7; i++)
            {
                RadTextBox txt1 = (RadTextBox)gv_Evaluation.Rows[i].Cells[3].FindControl("txt_evaluateWeights");
                int weight;
                bool result = Int32.TryParse(txt1.Text, out weight);
                if (result)
                {
                    sum += weight;
                }
            }

            double percentage;
            percentage = Math.Round((sum / 100) * 100, 2);
            RadTextBox txt3 = (RadTextBox)gv_Evaluation.Rows[7].Cells[3].FindControl("txt_evaluateWeights");
            txt3.Text = percentage.ToString();//string.Format("{0:0.0%}", percentage.ToString());

        }

       protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e)

        {
            calc();
        }

My aspx:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Panel ID="pnl_research" runat="server" CssClass="pnl">
                <div id="detailsDiv" align="center" style="width: 800px;">
                    <table border="0" width="98%">
                        <tr>
                            <td align="center">
                                <asp:Panel ID="panel_rmv" runat="server" Visible="true" Direction="RightToLeft">
                                    <div class="grid" dir="rtl">
                                        <div class="grid" dir="rtl">
                                            <div class="rounded">
                                                <div class="top-outer">
                                                    <div class="top-inner">
                                                        <div class="top">
                                                            <h2>
                                                                <asp:Label ID="Label35" runat="server" Text="##"></asp:Label></h2>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="mid-outer">
                                                    <div class="mid-inner">
                                                        <div class="mid">
                                                            <asp:GridView Width="100%" ID="gv_Evaluation" CssClass="datatable" AllowSorting="True"
                                                                runat="server" TabIndex="2" AutoGenerateColumns="False" AllowPaging="True" GridLines="None"
                                                                OnRowDataBound="gv_Evaluation_RowDataBound">
                                                                <EmptyDataTemplate>
                                                                    <table style="width: 100%;">
                                                                        <tr>
                                                                            <td>
                                                                            &nbsp;
                                                                        </tr>
                                                                        <tr>
                                                                            <td align="center">
                                                                                <asp:Label ID="Label4" runat="server" Font-Size="16pt" Text="&#1604;&#1575; &#1610;&#1608;&#1580;&#1583; &#1576;&#1610;&#1575;&#1606;&#1575;&#1578;"></asp:Label>
                                                                            </td>
                                                                        </tr>
                                                                        <tr>
                                                                            <td>
                                                                                &nbsp;
                                                                            </td>
                                                                        </tr>
                                                                    </table>
                                                                </EmptyDataTemplate>
                                                                <Columns>
                                                                    <asp:TemplateField HeaderText="م">
                                                                        <ItemTemplate>
                                                                            <asp:Label ID="lblSerial" runat="server"></asp:Label>
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>
                                                                    <asp:BoundField HeaderText="" DataField="activityType" />
                                                                    <asp:BoundField HeaderText="" DataField="activityWeight" />
                                                                    <asp:TemplateField HeaderText="">
                                                                        <ItemTemplate>
                                                                            <telerik:RadTextBox ID="txt_evaluateWeights" runat="server" AutoPostBack="True" OnTextChanged="txt_evaluateWeights_TextChanged">
                                                                            </telerik:RadTextBox>
                                                                        </ItemTemplate>
                                                                    </asp:TemplateField>
                                                                    <asp:BoundField HeaderText="" DataField="activitySelf" />
                                                                    <asp:BoundField HeaderText="" DataField="activityBoss" />
                                                                    <asp:BoundField HeaderText="" DataField="activityDean" />
                                                                </Columns>
                                                                <RowStyle VerticalAlign="Top" CssClass="row" />
                                                            </asp:GridView>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="bottom-outer">
                                                    <div class="bottom-inner">
                                                        <div class="bottom">
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                </asp:Panel>
                            </td>
                        </tr>
                    </table>
                </div>
            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>

Is the textbox in an UpdatePanel? Is the entire page getting posted?

You could set the focus in the code-behind...

protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e)
{
    calc();
    ((TextBox)sender).Focus();
}

You can use jquery to do it

$('#txt_evaluateWeights').focus();

or normal javascript

document.getElementById("Box1").focus();
protected void TxtPaidAmtTextChanged(object sender, EventArgs e)
{
    int index = ((TextBox)sender).TabIndex;
    TextBox txtindex = (TextBox)gridCurrentFeeHead.Rows[index + 1].FindControl("TxtPaidAmt");
    txtindex.Focus();
}

Thanks a lot, I fix my problem:

Firstly:

because no command argument property for the textbox to store the gridview index, i store it in the tab index.

TabIndex='<%#((GridViewRow)Container).RowIndex%>'

 protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e)
        {
            calc();
           int index = ((RadTextBox)sender).TabIndex;
           ((RadTextBox)gv_Evaluation.Rows[index + 1].Cells[3].FindControl("txt_evaluateWeights")).Focus();
        }

I am not sure if this is what you are trying to accomplish. The problem I had was changing the value of a text box has affects on other text boxes.

When the user tries to type in 1.23 each time, the text change function gets called, and it puts the cursor at the beginning of the box. So I would end up with 32.1 in the box.

To solve this I made a copy of the text in the box. Then on textChanged, I did a character by character comparison to the saved text with the current, to get the current cursor location.

Then at the end of the TextChanged function I re-saved the new text and set the cursor back to the spot is was before the call. using:

thisTextBox.Select(cursorLocation, 0);

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