如何在文本更改事件后保持对文本框的关注

本文关键字:文本 事件 | 更新日期: 2023-09-27 18:02:43

我有一个gridview,其中包含一个文本框作为模板字段。网格视图位于更新面板中。

我使用文本更改事件来计算前四个文本框的百分比,并将结果放在第五个文本框中,我的问题是:一旦文本更改,我总是失去焦点,每次我都应该将鼠标光标再次移动到目标文本框。如何解决这个问题?我想在文本更改后将焦点放在文本框上。

我代码:

 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();
        }

我的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>

如何在文本更改事件后保持对文本框的关注

文本框是否在UpdatePanel中?整个页面都被发布了吗?

你可以在代码后面设置焦点…

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

可以使用jquery实现

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

或普通javascript

document.getElementById("Box1").focus();

非常感谢,我解决了我的问题:

首先

:

因为文本框没有命令参数属性来存储gridview索引,所以我将其存储在TAB索引中。

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();
        }
protected void TxtPaidAmtTextChanged(object sender, EventArgs e)
{
    int index = ((TextBox)sender).TabIndex;
    TextBox txtindex = (TextBox)gridCurrentFeeHead.Rows[index + 1].FindControl("TxtPaidAmt");
    txtindex.Focus();
}

我不确定这是否是您想要完成的。我遇到的问题是改变一个文本框的值会影响其他文本框。

当用户每次尝试键入1.23时,将调用文本更改函数,并将光标置于框的开头。所以我最终会得到32.1。

为了解决这个问题,我复制了框中的文本。然后在textChanged上,我对保存的文本与当前文本进行逐个字符的比较,以获得当前光标的位置。

然后在TextChanged函数结束时,我重新保存新文本并将光标设置回调用前的位置。使用:

thisTextBox.Select(cursorLocation, 0);