如何在文本更改事件后将焦点保持在文本框上

时间:2011-07-19 14:16:42

标签: c# asp.net ajax gridview updatepanel

我有一个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>

5 个答案:

答案 0 :(得分:4)

UpdatePanel中的文本框是?整个页面是否已发布?

您可以在代码隐藏中设置焦点......

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

答案 1 :(得分:3)

您可以使用jquery来执行此操作

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

或普通的javascript

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

答案 2 :(得分:1)

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

首先:

因为没有用于存储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();
        }

答案 3 :(得分:1)

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

答案 4 :(得分:0)

我不确定这是否是您要完成的任务。 我更改文本框的值时遇到的问题影响了其他文本框。

当用户每次尝试输入1.23时,都会调用文本更改功能,并将光标置于框的开头。所以我最终会在框中输入32.1。

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

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

thisTextBox.Select(cursorLocation, 0);