繁体   English   中英

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

[英]How to keep focus on the text box after text changed event

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

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

我的代码:

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

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

首先:

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

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

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

当用户每次尝试输入 1.23 时,会调用文本更改 function,并将 cursor 放在框的开头。 所以我最终会在盒子里得到 32.1。

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

然后在 TextChanged function 结束时,我重新保存了新文本并将 cursor 设置回原点是在调用之前。 使用:

thisTextBox.Select(cursorLocation, 0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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