【问题标题】:How to keep focus on the text box after text changed event文本更改事件后如何保持对文本框的关注
【发布时间】:2011-10-08 13:59:55
【问题描述】:

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

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

我的代码:

 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>

【问题讨论】:

    标签: c# asp.net ajax gridview updatepanel


    【解决方案1】:

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

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

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

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

    thisTextBox.Select(cursorLocation, 0);
    

    【讨论】:

      【解决方案2】:
      protected void TxtPaidAmtTextChanged(object sender, EventArgs e)
      {
          int index = ((TextBox)sender).TabIndex;
          TextBox txtindex = (TextBox)gridCurrentFeeHead.Rows[index + 1].FindControl("TxtPaidAmt");
          txtindex.Focus();
      }
      

      【讨论】:

        【解决方案3】:

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

        首先:

        因为文本框没有命令参数属性来存储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();
                }
        

        【讨论】:

          【解决方案4】:

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

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

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

          【讨论】:

          • hmmm ,在我点击tab 之后,结果出现在最后一个文本框中,焦点返回到sender 但我想要在我点击tab 后的文本框。因为用户已经在这个文本框中写了。
          • 我的意思是,我在第一个文本框中输入数据。通过tab 移动到下一个文本框后。焦点回到第一个,而不是我想要处理的第二个。
          • 我的意思是键盘上的按钮移动到下一个文本框
          • 我更新了我的问题,并把整个更新面板。以澄清结构。
          【解决方案5】:

          你可以用jquery来做

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

          或普通的javascript

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

          【讨论】:

          • 你应该在文本框上添加一个onblur事件,并在里面写上上面的代码
          • 它是 asp:TextBox(服务器控件)。
          • 是的,我知道它是一个 asp:textbox,你可以在上面添加一个 onblur 事件。你试过了吗?
          猜你喜欢
          • 2016-02-15
          • 2014-01-26
          • 1970-01-01
          • 1970-01-01
          • 2016-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多