1

我的屏幕上有 2 个更新面板。一个有一个文本框,另一个是gridview。我有一个计时器(在 UpdatePanel 之外),它每 5 秒刷新一次 gridview。包含文本框的 UpdatePanel 有一个按钮(在 UpdatePanel 之外),当按下时,在文本框中输入的内容被添加到 DB 中,并且文本框被清除,所有这些都通过 UpdatePanel(ajax,没有页面加载)。

我遇到的问题是,当链接到计时器并刷新 gridview 的 UpdatePanel 时,它会将焦点从文本框中转移开。我可以通过添加 codebdehind "System.Web.UI.ScriptManager.GetCurrent(this).SetFocus(this.txtNewComment);" 来关注它 但这会将光标放在文本框的开头。如果我正在输入一些东西,它会搞砸我正在输入的内容,因为光标位于开头而不是结尾。

当计时器触发UpdatePanel时,关于如何将光标准确地保持在文本框中的任何想法?

通常,您会使用Select()此处所述的文本框功能(http://msdn.microsoft.com/en-us/library/ms752349.aspx),但在 MS 的 ASP.NET 中似乎不存在 Select()纽扣。

4

2 回答 2

2

尝试将属性设置为两个 UpdatePanel UpdateMode="Conditional"

在以下示例中,文本框不会失去焦点。

<asp:UpdatePanel ID="TextBoxUpdatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:TextBox ID="PanelTextBox" runat="server"></asp:TextBox>
    </ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Literal ID="TimeLiteral" runat="server"></asp:Literal>
        <asp:Timer ID="myTimer" runat="server" OnTick="myTimer_Tick">
        </asp:Timer>
    </ContentTemplate>
</asp:UpdatePanel>

以及背后的代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack) {
        PanelTextBox.Focus();
        myTimer.Interval = 3000;
        myTimer.Enabled = true;
    }
}

protected void myTimer_Tick(object sender, EventArgs e)
{
    System.Threading.Thread.Sleep(2000);
    TimeLiteral.Text = DateTime.UtcNow.Ticks.ToString();
}
于 2013-10-07T20:48:10.423 回答
0

将 TextBox 放在 UpdatePanel 之外。

<asp:TextBox ID="PanelTextBox" runat="server"></asp:TextBox>
<asp:UpdatePanel ID="TextBoxUpdatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
      ...
    </ContentTemplate>
</asp:UpdatePanel>
于 2019-04-13T12:23:07.697 回答